php - GROUP_CONCAT returns 1 row for 0 results -


i hope makes sense.

i have query i'm running through php , part of query, i'm using group_concat. works great , want if results empty, still returns 1 results series of null values. know group_concat affecting because if remove query, issue doesn't happen.

also, i'm aware can fix php reading first variable in array, checking null value assuming it's empty string i'm more curious why happens , if there better way writing sql here.

i don't know if here's 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) distance,         a.add1, a.add2, a.city cityother, a.state stateother, a.zip 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)) alcoholarray,         group_concat(distinct(rc.name)) cuisinearray,         group_concat(distinct(rd.name)) diningarray,         k.id kidsmenu          20_00_locations m          /*address*/         left join 20_01_addresses         on a.restid = m.id         , a.active = 1         , 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         , p.active = 1         , p.type = 1          /*restaurant hours*/         left join 60_20_1_hours h         on m.hourstempid = h.tempid         , h.daynum = 1 /*assigned dynamically*/          /*check 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         , k.active = 1         , k.optid = 8         , k.typeid = 29          /*config used cuisine, alcohol, dining, etc*/         left join 20_02_config c         on c.restid = m.id         , c.active = 1          /*cusine types*/         left join 80_00_master rc         on rc.intid = c.optid         , rc.parid = 29         , c.typeid = 29         , c.optid <> 8          /*alcohol types*/         left join 80_00_master ra         on ra.intid = c.optid         , ra.parid = 30         , c.typeid = 30          /*dining types*/         left join 80_00_master rd         on rd.intid = c.optid         , rd.parid = 31         , c.typeid = 31          /*menu table*/         (acos(sin(:lat)*sin(radians(m.lat)) + cos(:lat)*cos(radians(m.lat))*cos(radians(m.lng)-:lon)) * :r) < :rad         , m.lat between :minlat , :maxlat         , m.lng between :minlon , :maxlon         , m.active = 1         , m.published = 1          order distance 

few things query, said don't have group selecting non-aggregated columns:

select   m.id, m.rnid, m.displaylogopath, m.displayname, m.tagline, ... 

and using aggregated functions group_concat:

group_concat(distinct(ra.name)) alcoholarray, group_concat(distinct(rc.name)) cuisinearray, group_concat(distinct(rd.name)) diningarray, 

so query return 1 row, values of m.id, m.rnid, etc. undetermined (they might first row, or other row).

so might want remove non-aggregated columns, , use having clause:

select group_concat(...) ... having count(*)>0 

but! missing group by, think should enough:

group m.id 

please note not sql-compliant mysql happily execute it, , (if understand correctly) if not considered practice return right result.

but prefer rewrite query this:

select   m.id, ...,   ra.alcoholarray,   rc.cuisinearray,   ...   20_00_locations m left join 20_01_addresses on ...   left join 80_00_geo_data g on ...   ...   left join (     select intid, group_concat(name) cuisinearray     80_00_master           rc.parid = 29     group intid   ) rc on rc.intid = c.optid ... 

using subqueries.


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -