GROUP_CONCAT为0结果返回1行

时间:2015-09-30 20:11:06

标签: php mysql group-concat

我希望这是有道理的。

我有一个查询,我正在通过PHP运行,作为我的查询的一部分,我正在使用GROUP_CONCAT。它工作得很好并且可以完成我想要的所有操作但是如果结果为空,它仍然会返回带有一系列NULL值的1个结果。我知道这是影响这个的GROUP_CONCAT,因为如果我从查询中删除它,问题就不会发生。

另外,我非常清楚我可以通过读取数组中的第一个变量,检查空值然后假设它是一个空字符串来解决这个问题,但我更好奇为什么会发生这种情况如果有更好的方法,我可以在这里编写我的SQL。

我不知道它是否会有所帮助,但这是我的查询

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

1 个答案:

答案 0 :(得分:2)

您的查询很少,您说您没有GROUP BY但是您正在选择一些非聚合列:

SELECT
  m.id, m.RNID, m.DisplayLogoPath, m.DisplayName, m.TagLine, ...

您正在使用一些聚合函数GROUP_CONCAT:

GROUP_CONCAT(DISTINCT(ra.Name)) as AlcoholArray,
GROUP_CONCAT(DISTINCT(rc.Name)) AS CuisineArray,
GROUP_CONCAT(DISTINCT(rd.Name)) AS DiningArray,

因此您的查询将始终返回1行,但m.id,m.RNID等的值将不确定(它们可能来自第一行,或来自任何其他行)。

因此,您可能希望删除所有非聚合列,并使用HAVING子句:

SELECT GROUP_CONCAT(...)
FROM ...
HAVING COUNT(*)>0

BUT!你可能只是错过了GROUP BY,我认为这应该足够了:

GROUP BY m.id

请注意,这不符合SQL,但MySQL会愉快地执行它,并且(如果我理解正确的话)即使它不被认为是好的做法,它也会返回正确的结果。

但我更愿意像这样重写你的查询:

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
...

使用子查询。