mySQL GROUP_CONCAT - 查询

时间:2015-11-29 15:36:17

标签: mysql sql database

我可以从sql查询得到几乎所需要的但不完全的,我会很感激帮助。

我有以下三个表:

people
+----+-------------------------+
| id | email                   | 
+----+-------------------------+
|  1 | joe_soap@hotmail.com    |
|  2 | john_doe@hotmail.com    |
|  3 | fred_bloggs@hotmail.com |
+----+-------------------------+

jobs
+----+-------------+
| id | description | 
+----+-------------+
|  1 | Plumber     | 
|  2 | Plasterer   | 
|  3 | Carpenter   | 
|  4 | Builder     | 
+----+-------------+

people_jobs
+-----------+--------+
| person_id | job_id | 
+-----------+--------+
|     1     |   1    | 
|     1     |   3    | 
|     2     |   3    | 
|     3     |   3    | 
|     3     |   1    |
|     3     |   4    |
+-----------+--------+ 

使用此查询,我可以以我需要的格式输出所有数据:

SELECT people.id, people.email,
GROUP_CONCAT(DISTINCT jobs.description 
ORDER By jobs.description DESC SEPARATOR ', ')
FROM jobs
INNER JOIN people_jobs On people_jobs.job_id = jobs.id 
INNER JOIN people On people.id = people_jobs.person_id
GROUP BY people.id

如下:

+----+-------------------------+----------------------------+
| id |         email           | GROUP_CONCAT               |
+----+-------------------------+----------------------------+
|  1 | joe_soap@hotmail.com    | Plumber, Carpenter         |
|  2 | john_doe@hotmail.com    | Carpenter                  |
|  3 | fred_bloggs@hotmail.com | Plumber, Carpenter,Builder |
+----+-------------------------+----------------------------+

通过添加WHERE子句,我可以选择所有可以执行特定工作的人员:

SELECT people.id, people.email,
GROUP_CONCAT(DISTINCT jobs.description 
ORDER By jobs.description DESC SEPARATOR ', ')
FROM jobs
INNER JOIN people_jobs On people_jobs.job_id = jobs.id 
INNER JOIN people On people.id = people_jobs.person_id
WHERE jobs.description = 'Plumber'
GROUP BY people.id

+----+-------------------------+--------------+
| id |         email           | GROUP_CONCAT |
+----+-------------------------+--------------+
|  1 | joe_soap@hotmail.com    | Plumber      |
|  3 | fred_bloggs@hotmail.com | Plumber      |
+----+-------------------------+--------------+

我想要实现的是上述输出以及人们可以执行的其他工作,如下所示:

+----+-------------------------+----------------------------+
| id |         email           | GROUP_CONCAT               |
+----+-------------------------+----------------------------+
|  1 | joe_soap@hotmail.com    | Plumber, Carpenter         |
|  3 | fred_bloggs@hotmail.com | Plumber, Carpenter,Builder |
+----+-------------------------+----------------------------+

有人可以帮忙吗?

3 个答案:

答案 0 :(得分:2)

改为使用HAVING子句:

SELECT people.id, people.email,
       GROUP_CONCAT(DISTINCT jobs.description 
       ORDER By jobs.description DESC SEPARATOR ', ')
FROM jobs
INNER JOIN people_jobs On people_jobs.job_id = jobs.id 
INNER JOIN people On people.id = people_jobs.person_id
GROUP BY people.id
HAVING COUNT(CASE WHEN jobs.description = 'Plumber' THEN 1 END) >= 1

HAVING过滤掉群组,其中至少有一个作业说明不等于'Plumber',而WHERE每行操作,不包括所有没有等于'Plumber'的职位描述。

Demo here

答案 1 :(得分:1)

您可以使用子查询打包第一个查询并使用FIND_IN_SET

SELECT *
FROM (
   SELECT people.id, people.email,
     GROUP_CONCAT(DISTINCT jobs.description 
     ORDER By jobs.description DESC SEPARATOR ', ') AS jobs
   FROM jobs
   JOIN people_jobs On people_jobs.job_id = jobs.id 
   JOIN people On people.id = people_jobs.person_id
   GROUP BY people.id) AS sub
WHERE FIND_IN_SET('Plumber', sub.jobs) > 0;

SqlFiddleDemo

但在Giorgos Betsos回答中它会慢于HAVING方法。

答案 2 :(得分:1)

在MySQL中,您可以添加一个简单的having子句,类似于where子句:

SELECT p.id, p.email,
       GROUP_CONCAT(DISTINCT j.description ORDER By j.description DESC SEPARATOR ', ')
FROM jobs j INNER JOIN
     people_jobs pj
     On pj.job_id = j.id INNER JOIN
     people p
     On p.id = pj.person_id
GROUP BY p.id
HAVING SUM(j.description = 'Plumber') > 0;

此外,您可能不需要DISTINCT中的GROUP_CONCAT(),除非您的表格有重复项。