Php 如果组中有一个条目为空,mysql组_concat将返回空行
我有一个相当简单的查询,它将来自另一个表的所有结果合并到一个json对象中 MySQLPhp 如果组中有一个条目为空,mysql组_concat将返回空行,php,mysql,concatenation,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.lo
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;
如果组内的位置
结果中的任何一个为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返回的值周围添加引号,您可以使用如下方法:
'\"lat\":',concat('"',coalesce(l.lat,'Unknown'),'"')
你也许在找不
我也是
CONCAT('{',
'\"lat\":',l.lat,
',\"lon\":',l.lo
到目前为止,福拉夫的答案是有效的。。。但也许我需要urs anywayCONCAT,在查询的位置部分,每次都会用{
进行包装,基本上是为我要合并的每一行开始整个过程。好东西,所有内容都正确返回。既然我有你,有没有办法添加“
在合并之前并在合并中添加结果?我假设不是:合并(\'l.lat\','null')
将返回\'l.lat\'”
,这很有意义,谢谢。这个想法正是我想要的,但是有没有办法让未知的\'
。目前,返回的结果将始终带有引号。我只希望非null的结果带有引号。现在可以了,我将在php中使用一个简单的字符串替换来删除null值中的“
。谢谢:)
CONCAT('{',
'\"lat\":',l.lat,
',\"lon\":',l.lo