Wednesday, 15 May 2013

mysql - Include null results in group_concat -



mysql - Include null results in group_concat -

i have 2 tables this

profile_answers

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

educations

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

i ran query,

select educations.profile_answer_id, group_concat(educations.sample) educations left bring together profile_answers on educations.profile_answers_id = profile_answers.id

i got

+--------+--------------------+-------------+ | id | sample | +---------+--------------------+------------+ | 1 | 1234,2334,3434 | +---------+------------+--------------------+

i want,

+--------+--------------------+-------------+ | id | sample | +---------+--------------------+------------+ | 1 | 1234,2334,3434 | | 2 | null | | 3 | null | +---------+------------+--------------------+

select id,ifnull(samples,'null') sample ( select aa.id, group_concat(distinct bb.sample) samples profile_answers aa left bring together educations bb on aa.id = bb.profile_answers_id grouping aa.id ) a;

mysql group-concat isnull ifnull

No comments:

Post a Comment