I hope this makes sense.
I have a query that I'm running through PHP and as part of my query, I'm using GROUP_CONCAT. It works great and does everything I want but if the results are empty, it still returns 1 results with a series of NULL values. I know it is GROUP_CONCAT affecting this because if I remove it from the query, the issue doesn't happen.
Also, I'm very well aware that I can simply fix this with PHP by reading the first variable in the array, checking for a null value then assuming it's an empty string but I'm more curious as to why this happens and if there is a better way I could be writing my SQL here.
I don't know if it will help but here's my query
SELECT
m.id, m.RNID, m.DisplayLogoPath, m.DisplayName, m.TagLine,
(acos(sin(:lat)*sin(radians(m.Lat)) + cos(:lat)*cos(radians(m.Lat))*cos(radians(m.Lng)-:lon)) * :R) As Distance,
a.Add1, a.Add2, a.City as CityOther, a.State as StateOther, a.Zip as ZipOther,
g.primary_city, g.state, g.zip,
p.AreaCode, p.Prefix, p.LineNum,
h.Open, h.Close, h.Open24, h.Closed24,
GROUP_CONCAT(DISTINCT(ra.Name)) as AlcoholArray,
GROUP_CONCAT(DISTINCT(rc.Name)) AS CuisineArray,
GROUP_CONCAT(DISTINCT(rd.Name)) AS DiningArray,
k.id AS KidsMenu
FROM 20_00_locations m
/*Address*/
LEFT JOIN 20_01_addresses a
ON a.RestID = m.id
AND a.active = 1
AND a.Type = 1
/*Geographic ref data*/
LEFT JOIN 80_00_geo_data g
ON g.id = a.CSZID
/*Phone*/
LEFT JOIN 20_01_phones p
ON p.RestID = m.id
AND p.active = 1
AND p.Type = 1
/*Restaurant hours*/
LEFT JOIN 60_20_1_hours h
ON m.HoursTempID = h.TempID
AND h.DayNum = 1 /*Assigned dynamically*/
/*Check the kids menu status - if Null, no kids menu. If id has value, kids menu*/
LEFT JOIN 20_02_config k
ON k.RestID = m.id
AND k.active = 1
AND k.OptID = 8
and k.TypeID = 29
/*Config used to get cuisine, alcohol, dining, etc*/
LEFT JOIN 20_02_config c
ON c.RestID = m.id
AND c.active = 1
/*Cusine types*/
LEFT JOIN 80_00_master rc
ON rc.IntID = c.OptID
AND rc.ParID = 29
AND c.TypeID = 29
AND c.OptID <> 8
/*Alcohol types*/
LEFT JOIN 80_00_master ra
ON ra.IntID = c.OptID
AND ra.ParID = 30
AND c.TypeID = 30
/*Dining types*/
LEFT JOIN 80_00_master rd
ON rd.IntID = c.OptID
AND rd.ParID = 31
AND c.TypeID = 31
/*Menu table*/
WHERE (acos(sin(:lat)*sin(radians(m.Lat)) + cos(:lat)*cos(radians(m.Lat))*cos(radians(m.Lng)-:lon)) * :R) < :rad
AND m.Lat Between :minLat And :maxLat
AND m.Lng Between :minLon And :maxLon
AND m.active = 1
AND m.Published = 1
ORDER BY Distance
Few things about your query, you said that you don't have a GROUP BY but you are SELECTing some non-aggregated columns:
SELECT
m.id, m.RNID, m.DisplayLogoPath, m.DisplayName, m.TagLine, ...
and you are using some aggregated functions GROUP_CONCAT:
GROUP_CONCAT(DISTINCT(ra.Name)) as AlcoholArray,
GROUP_CONCAT(DISTINCT(rc.Name)) AS CuisineArray,
GROUP_CONCAT(DISTINCT(rd.Name)) AS DiningArray,
so your query will always return 1 row, but the values of m.id, m.RNID, etc. will be undetermined (they might be from the first row, or from any other row).
So you might want to remove all non-aggregated columns, and use an HAVING clause:
SELECT GROUP_CONCAT(...)
FROM ...
HAVING COUNT(*)>0
BUT! you are probably just missing a GROUP BY, I think this should be enough:
GROUP BY m.id
please note that this is not a SQL-compliant but MySQL will happily execute it, and (if I understand correctly) even if it is not considered good practice it will return the right result.
But I would prefer to rewrite your query as this:
SELECT
m.id, ...,
ra.AlcoholArray,
rc.CuisineArray,
...
FROM
20_00_locations m LEFT JOIN 20_01_addresses a ON ...
LEFT JOIN 80_00_geo_data g ON ...
...
LEFT JOIN (
SELECT IntID, GROUP_CONCAT(Name) AS CuisineArray
FROM 80_00_master
WHERE
rc.ParID = 29
GROUP BY IntID
) rc ON rc.IntID = c.OptID
...
using subqueries.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.