简体   繁体   English

仅GROUP_CONCAT活动功能

[英]Only GROUP_CONCAT active functions

I have four tables, a clients , persons , client_functions and functions table. 我有四个表,一个clientspersonsclient_functionsfunctions表。

I wrote this query: 我写了这个查询:

SELECT 

P.number,
P.first_name
GROUP_CONCAT(F.description) AS Functions

FROM clients AS C

LEFT JOIN persons AS P ON P.id=C.id
LEFT JOIN client_functions as CF ON CF.client_id=C.id
LEFT JOIN functions AS F ON F.id=CF.function_id

WHERE P.person_type = 'client' AND P.company_id = 3

GROUP BY

P.number,
P.first_name

In my GROUP_CONCAT() i only want to group F.description if CF.archived = 0. Does anybody has an idea on how i can put a condition on the GROUP_CONCAT? 在我的GROUP_CONCAT()中,如果CF.archived = 0,我只想将F.description分组。有人对我如何在GROUP_CONCAT上加条件有想法吗?

Current query results in: 当前查询结果:

--------------------------------------------
| 93    | Jan Lochtenberg   | PV,PV,PV,PV   |
| 94    | Chris van Eijk    | VP-I,VP,PV    |
| 95    | Gertrude Irene    | VP-I,PV,PV,PV |
| 96    | Wiekert Jager     | VP-I,PV       |
| 97    | Antonius Kode     | VP,VP-I,VP    |
| 98    | HansLelie         | PV,PV,PV      |
---------------------------------------------

But i only want to see the active functions 但是我只想看到活动功能

--------------------------------------------
| 93    | Jan Lochtenberg   | PV            |
| 94    | Chris van Eijk    | VP-I,VP,PV    |
| 95    | Gertrude Irene    | VP-I,PV       |
| 96    | Wiekert Jager     | VP-I,PV       |
| 97    | Antonius Kode     | VP,VP-I,VP    |
| 98    | HansLelie         | PV            |
---------------------------------------------

Your where is undoing some of your left join s. 您的where正在撤消一些left join联接。 In fact, you don't need the clients table at all. 实际上,您根本不需要clients表。 Then you can put the filtering condition on functions in the ON clause: 然后可以将过滤条件放在ON子句中的函数上:

SELECT P.number, P.first_name, P.last_name,
       GROUP_CONCAT(F.description) AS Functions
FROM persons P LEFT JOIN
     client_functions CF
     ON CF.client_id = p.id LEFT JOIN
     functions F
     ON F.id = CF.function_id AND cf.archived = 0
WHERE P.person_type = 'client' AND P.company_id = 3
GROUP BY P.number, P.first_name, P.last_name;

In my GROUP_CONCAT() i only want to group F.description if CF.archived = 0 在我的GROUP_CONCAT() ,如果CF.archived = 0我只想将F.description CF.archived = 0

Translated to SQL: 转换为SQL:

GROUP_CONCAT(IF(CF.archived = 0, F.description, NULL))

The GROUP_CONCAT() function ignores the NULL values. GROUP_CONCAT()函数将忽略NULL值。 It returns, however, NULL if there isn't any not- NULL value to work with. 但是,如果没有可使用的非NULL值,它将返回NULL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM