如果组中的一个条目为null,则mysql group_concat返回空行

时间:2014-05-21 13:09:05

标签: php mysql concatenation

我有一个相当简单的查询,它希望将另一个表中的所有结果合并到一个json对象中。

的MySQL

SELECT s.id, s.uid, s.sexnumber, s.rating, s.sextime, s.diary, GROUP_CONCAT(p.pid,',') as positions, GROUP_CONCAT(w.id,':',w.who) as who, GROUP_CONCAT('{',
        '\"lat\":',l.lat,
        ',\"lon\":',l.lon,
        ',\"house\":',l.house,
        ',\"house_number\":',l.house_number,
        ',\"road\":',l.road,
        ',\"supermarket\":',l.supermarket,
        ',\"city\":',l.city,
        ',\"city_district\":',l.city_district,
        ',\"country\":',l.country,
        ',\"country_code\":',l.country_code,
        ',\"county\":',l.county,
        ',\"neighbourhood\":',l.neighbourhood,
        ',\"pedestrian\":',l.pedestrian,
        ',\"place_of_worship\":',l.place_of_worship,
        ',\"postcode\":',l.postcode,
        ',\"state\":',l.state,
        ',\"suburb\":',l.suburb,
        '}'
    ) as location, GROUP_CONCAT(ww.id,':',ww.name) as place
    FROM users u join sex s
     on s.uid = u.uid
     LEFT OUTER JOIN whos ws
     ON s.id = ws.sid
     LEFT OUTER JOIN who w
     ON w.id = ws.wid
     LEFT OUTER JOIN locations l
     ON l.sid = s.id
     LEFT OUTER JOIN wheresex whs
     ON whs.sid = s.id
     LEFT OUTER JOIN wherewhere ww
     ON whs.wid = ww.id
     LEFT OUTER JOIN positions p
     ON s.id = p.sid
    WHERE u.sessionCheck = '%s'
    GROUP BY s.id
    ORDER BY s.sextime DESC;

如果location中的任何GROUP_CONCAT结果为NULL,则整个条目将返回NULL

如何让NULL的个别结果以这种方式返回以及其他任何值的结果?

2 个答案:

答案 0 :(得分:2)

我必须承认我以前没有尝试过执行这种查询,但是我看不出合并会怎么做呢?

SELECT s.id, s.uid, s.sexnumber, s.rating, s.sextime, s.diary, GROUP_CONCAT(p.pid,',') as positions, GROUP_CONCAT(w.id,':',w.who) as who, GROUP_CONCAT('{',
        '\"lat\":',coalesce(l.lat,'Unknown'),
        ',\"lon\":',coalesce(l.lon,'Unknown'),
        ',\"house\":',coalesce(l.house,'Unknown'),
        ',\"house_number\":',coalesce(l.house_number,'Unknown'),
        ',\"road\":',coalesce(l.road,'Unknown'),
        ',\"supermarket\":',coalesce(l.supermarket,'Unknown'),
        ',\"city\":',coalesce(l.city,'Unknown'),
        ',\"city_district\":',coalesce(l.city_district,'Unknown'),
        ',\"country\":',coalesce(l.country,'Unknown'),
        ',\"country_code\":',coalesce(l.country_code,'Unknown'),
        ',\"county\":',coalesce(l.county,'Unknown'),
        ',\"neighbourhood\":',coalesce(l.neighbourhood,'Unknown'),
        ',\"pedestrian\":'coalesce(,l.pedestrian,'Unknown'),
        ',\"place_of_worship\":',coalesce(l.place_of_worship,'Unknown'),
        ',\"postcode\":',coalesce(l.postcode,'Unknown'),
        ',\"state\":',coalesce(l.state,'Unknown'),
        ',\"suburb\":',coalesce(l.suburb,'Unknown'),
        '}'
    ) as location, GROUP_CONCAT(ww.id,':',ww.name) as place

编辑:我不确定我是否正确理解您的评论,但如果您想在合并返回的值周围添加引号,则可以使用concat function,如下所示:

'\"lat\":',concat('"',coalesce(l.lat,'Unknown'),'"')

答案 1 :(得分:1)

您可能正在寻找Concat而不是Group_Concat

   CONCAT(w.id,':',w.who)

相同
  CONCAT('{',
    '\"lat\":',l.lat,
    ',\"lon\":',l.lo