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


SQL query to search for user tokens(skills)

我正在我的网站上建立一个搜索系统,搜索用户技能,这些技能被表示为令牌,用户技能的数据库表看起来像这样:

 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命令

这是该命令的详细信息的链接

http://www.w3resource.com/mysql/aggregate-functions-and-grouping/aggregate-functions-and-grouping-group_concat.php

希望对你有帮助。

谢谢。