Php 用于搜索用户令牌(技能)的SQL查询

Php 用于搜索用户令牌(技能)的SQL查询,php,jquery,mysql,jquery-tokeninput,Php,Jquery,Mysql,Jquery Tokeninput,我正在我的网站上建立一个搜索系统,搜索用户技能。此技能以令牌形式显示,用户技能的db表如下所示: id=> primary id(auto increment), user_id=> this is the user id, competence_nom=> this is the skill name, competence_id=> this is the skill parent id user name + skill one + skill two

我正在我的网站上建立一个搜索系统,搜索用户技能。此技能以令牌形式显示,用户技能的db表如下所示:

 id=> primary id(auto increment), 
user_id=> this is the user id,  
competence_nom=> this is the skill name, 
competence_id=> this is the skill parent id
user name + skill one + skill two
所以我想要的是当两个或更多的技能。。属于一个用户显示如下:

 id=> primary id(auto increment), 
user_id=> this is the user id,  
competence_nom=> this is the skill name, 
competence_id=> this is the skill parent id
user name + skill one + skill two
而不是我现在所取得的成就

user name +skill one
user name +skill two
我使用jQuery tokeninput插件将令牌数据传递给服务器端,我将技能id传递给服务器端,然后在服务器端分解jQuery tokeninput给出的技能数组:

 $comps=explode(",", $_POST["competences"]); 
然后我将其放入foreach循环中:

 foreach($comps as $i=>$v){  


}
这就是我得到错误输出的地方:

user name +skill one
    user name +skill two

据我目前的理解,这是您的数据模型的一个问题

核心问题是您的数据模型必须支持哪些用例,以及底层查询的效率

如果您的技能集非常有限,并且需要非常高的性能,那么您可能希望采用一种解决方案,将技能集真正表示为位集

如果清晰的用户界面和清晰的数据模型是主要目标,而性能可能是次要的,那么人们想到的方法是在一个表中建立一个关系数据模型,其中人员核心数据位于一个表中,在第二个表中分配技能,如下所示:

TABLE person
id: (auto increment)
name: (or split into first name etc)
email: (etc, you get the idea)

TABLE skill
id: (auto increment)
description:

TABLE personskill
personid: (reference to person.id)
skillid: (reference to skill.id)
然后,通过关系数据库查询,系统将回答以下问题:哪些技能分配给了哪些人

SELECT skillid FROM personskill WHERE personid = (id of person you're looking at)

您可以使用mysql的GROUP_CONCAT命令

下面是指向此命令详细信息的链接

希望这有帮助


谢谢。

我不认为一个mysql查询就能得到所需格式的结果。您需要获取数据并在php代码中重新构造它。是的,我正试图通过从sql查询填充数组来实现这一点,但仍然无法成功。您目前使用的查询是什么?此查询中有很多不一致的内容,但它给了我重复的模式:$a=$db->querySELECT*from competencies,competencies\u user,用户,其中users.id=用户id和$\u comps.=能力id和$\u comps.=c\u id和用户id=按c_id分组的0组;foreachi中的这个查询与您发布的表方案完全相同,我的问题来自php foreach i do服务器端,它重复数据:用户名+技能一个用户名+技能two@user7832好吧,然后我被一个事实误导了,你在personskill表中似乎有技能名称和自动递增id,我看不出有什么理由。所以我不会说你有我建议的确切的餐桌方案。哦,对不起,是的,我在personskill表中有一个自动增量id,因为人们可以动态添加技能,因此该技能可以添加到主技能表中,但也可以作为特定任务的技能user@user7832用户可以动态添加技能这一事实以何种方式成为表中具有自动递增id的原因,而该表只是将技能与一个人?还有,为什么要复制这个名字?不管有没有人工智能都不会影响任何事情,它可能是冗长的。名称的复制也是冗长的,但我不认为这会改变我进行查询的方式。