我有两张这样的桌子
个人资料回答

+---------+------------+
|      id | class_name |
+---------+------------+
|       1 | Class 1    |
|       2 | Class 2    |
|       3 | Class 1    |
+---------+------------+

教育
+---------+--------------------+------------+
|      id | profile_answers_id |  sample    |
+---------+--------------------+------------+
|       1 | 1                  |     1234   |
|       2 | 1                  |     2334   |
|       3 | 1                  |     3434   |
+---------+------------+--------------------+

我查了一下,
select educations.profile_answer_id, GROUP_CONCAT(educations.sample) from educations
LEFT JOIN profile_answers ON educations.profile_answers_id = profile_answers.id

我得到了
+--------+--------------------+-------------+
|      id | sample                          |
+---------+--------------------+------------+
|       1 | 1234,2334,3434                  |
+---------+------------+--------------------+

我真的想,
+--------+--------------------+-------------+
|      id | sample                          |
+---------+--------------------+------------+
|       1 | 1234,2334,3434                  |
|       2 | NULL                            |
|       3 | NULL                            |
+---------+------------+--------------------+

最佳答案

SELECT id,IFNULL(samples,'NULL') sample FROM
(
    SELECT
        AA.id,
        GROUP_CONCAT(DISTINCT BB.sample) samples
    FROM
        profile_answers AA LEFT JOIN educations BB
        ON AA.id = BB.profile_answers_id
    GROUP BY AA.id
) A;

关于mysql - 在group_concat中包含空结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14820298/

10-10 11:12