[英]Query with GROUP_CONCAT not working
I have 3 tables with the following structure: 我有3个具有以下结构的表:
**users**
id
first_name
last_name
**specialties**
specialty_id
specialty_name
**user_specialties**
user_id
specialty_id
Here is some sample data: 以下是一些示例数据:
**users**
1 Bill Smith
2 Tom Jones
3 Jill Hayes
**specialties**
1 word
2 web
3 database
**user_specialties**
1 1
2 1
2 3
3 2
3 3
I need to query the data so the specialties are concatinated on one row like the below output 我需要查询数据,以便将专长隐藏在一行上,如下面的输出所示
**Desired Result**
Bill Smith word
Tom Jones word,database
Jill Hayes web,database
I am using the following query 我正在使用以下查询
SELECT
users.first_name,
users.last_name,
GROUP_CONCAT(specialties.specialtyname)
FROM
users
LEFT JOIN user_specialties ON user_specialties.user_id = users.userid
RIGHT JOIN specialties ON user_specialties.specialty_id = specialties.specialty_id
It is not working... 它不起作用...
You're missing a GROUP BY
clause. 您缺少
GROUP BY
子句。 Most likely it should be GROUP BY users.id
, and it'd go AFTER the JOIN lines. 最有可能应该是
GROUP BY users.id
,它会在JOIN行之后。
I just tested this query 我刚刚测试了这个查询
SELECT first_name,last_name,group_concat(specialty_name)
FROM user_specialties map
INNER JOIN specialties skill on user.id = map.user_id
INNER JOIN users user ON skill.specialty_id = map.specialty_id
GROUP BY user.id
Cheers! 干杯! :-)
:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.