繁体   English   中英

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

[英]mysql group_concat returns null row if one entry in the group is null

我有一个相当简单的查询,它看起来将另一个表的所有结果合并到一个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;

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

如何以NULL返回单独的NULL结果以及其他任何值的值?

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

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

编辑:我不确定我是否正确理解您的评论,但如果您想在coalesce返回的值周围添加引号,您可以使用这样的concat函数

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

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

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

同样的

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

暂无
暂无

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

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