MySQL GROUP_CONCAT 查询排除记录

MySQL GROUP_CONCAT Query Excluding Records

我的以下查询有点问题。我从几个表中提取数据并对几行使用 GROUP_CONCAT 功能。 GROUP_CONCAT 基于 requests.REQUEST_ID 拉取自定义字段数据。但是,并非每个 REQUEST_IDTABLE_fields_data 中都有一个条目。因此,我的 WHERE 子句中的这个元素导致了一个问题:data.REQUEST_ID=requests.REQUEST_ID

即使TABLE_fields_data中没有相应的值,我仍然想从TABLE_requests中提取数据。在我当前的查询中,数据被完全省略了。

我已经在我的 GROUP_CONTACT 语句中尝试了 IFNULL,但这似乎没有什么不同。我已经尝试弄清楚如何在 data.REQUEST_ID=requests.REQUEST_ID WHERE 子句中执行 IF 语句,但也没有让它起作用。根据我下面的示例数据,查询不会 return 请求 ID 6000002。

非常坚持这一点,如有任何帮助,我们将不胜感激!

TABLE_requests
+------------+----------------+---------+---------------------+
| REQUEST_ID | REQUEST_STATUS | USER_ID | DATE_CREATE         |
+------------+----------------+---------+---------------------+
| 6000001    | COMPLETE       | 3000001 | 2020-06-01 11:15:16 |
+------------+----------------+---------+---------------------+
| 6000002    | COMPLETE       | 3000001 | 2020-06-02 16:14:11 |
+------------+----------------+---------+---------------------+
| 6000003    | PENDING        | 3000001 | 2020-06-03 12:11:11 |
+------------+----------------+---------+---------------------+

TABLE_fields
+----------+------+---------------+
| FIELD_ID | TYPE | LABEL_en      |
+----------+------+---------------+
| 1        | TEXT | Student Name  |
+----------+------+---------------+
| 2        | TEXT | Student Grade |
+----------+------+---------------+
| 3        | TEXT | Course        |
+----------+------+---------------+

TABLE_fields_data
+---------+------------+----------+------------+
| DATA_ID | REQUEST_ID | FIELD_ID | DATA       |
+---------+------------+----------+------------+
| 1       | 6000001    | 1        | John Smith |
+---------+------------+----------+------------+
| 2       | 6000001    | 2        | 1st Grade  |
+---------+------------+----------+------------+

SELECT 
    requests.REQUEST_ID, requests.REQUEST_STATUS, requests.USER_ID, requests.DATE_CREATE,
    GROUP_CONCAT(data.FIELD_ID SEPARATOR '||') AS CUSTOM_FIELD_ID,
    GROUP_CONCAT(fields.TYPE SEPARATOR '||') AS CUSTOM_FIELD_TYPE, 
    GROUP_CONCAT(fields.LABEL_en SEPARATOR '||') AS CUSTOM_FIELD_LABEL, 
    GROUP_CONCAT(data.DATA SEPARATOR '||') AS CUSTOM_FIELD_DATA
FROM TABLE_requests AS requests, TABLE_fields_data AS data, TABLE_fields AS fields
WHERE requests.REQUEST_STATUS='COMPLETE' AND data.REQUEST_ID=requests.REQUEST_ID AND fields.FIELD_ID=data.FIELD_ID GROUP BY requests.REQUEST_ID;

您正在使用 INNER 联接(旧式),但您需要的是 LEFT 联接:

SELECT 
    requests.REQUEST_ID, requests.REQUEST_STATUS, requests.USER_ID, requests.DATE_CREATE,
    GROUP_CONCAT(data.FIELD_ID SEPARATOR '||') AS CUSTOM_FIELD_ID,
    GROUP_CONCAT(fields.TYPE SEPARATOR '||') AS CUSTOM_FIELD_TYPE, 
    GROUP_CONCAT(fields.LABEL_en SEPARATOR '||') AS CUSTOM_FIELD_LABEL, 
    GROUP_CONCAT(data.DATA SEPARATOR '||') AS CUSTOM_FIELD_DATA
FROM TABLE_requests AS requests
LEFT JOIN TABLE_fields_data AS data ON data.REQUEST_ID = requests.REQUEST_ID
LEFT JOIN TABLE_fields AS fields ON fields.FIELD_ID = data.FIELD_ID
WHERE requests.REQUEST_STATUS = 'COMPLETE' 
GROUP BY requests.REQUEST_ID;