mysql - Select COUNT(*) zero if no joined results found -


i trying select areas given city id. in addition calculating count(*) see how many items existing each area. since items related areas via manytomany relationship, join has go via appropriate join-table called item_area.

unfortunately areas have recent items - want areas. recent items should simple zero, if none there. believe problem has where-condition published-date - cannot figure out how make work properly.

how can following query modified, select areas given area.cityid - , calculate "recentitems" = 0, areas don't have recent items (i.e. don't match restriction on item.published field within last 10 days.

select   `area`.`id`, `area`.`name`, count(*) `recentitems`   `area`   left join `item_area` on `area`.`id` = `item_area`.`areaid`   left join `item` on `item`.`id` = `item_area`.`itemid`   `area`.`cityid` = "1" ,   item.published > date_sub(now(), interval 10 day) group `area`.`id` order `area`.`name` asc 

move condition item where clause on clause. , change count(*) count(item.id):

select   area.id, area.name, count(item.id) recentitems area   left join item_area on area.id = item_area.areaid   left join item on  item.id = item_area.itemid                  , item.published > date_sub(now(), interval 10 day)    area.cityid = '1'  group    area.id order    area.name asc ; 

notice above not run under (strict) settings because have area.name in select , order by lists while isn't in group by list. depends on whether sql_mode has been set only_full_group_by. so, may wiser change group part to:

group    area.id, area.name 

you use aliases, make code more readable (at least lot of people):

select   a.id, a.name, count(i.id) recentitems      area   left join      item_area ia on a.id = ia.areaid   left join      item on  i.id = ia.itemid               , i.published > date_sub(now(), interval 10 day)    a.cityid = '1'  group    a.id order    a.name asc ; 

there @ least 2 more common ways write this. grouping first, in derived table, , joining:

select   a.id, a.name, coalesce(g.recentitems, 0) recentitems      area   left join      ( select          ia.areaid, count(*) recentitems                  item_area ia          join            item on  i.id = ia.itemid                i.published > date_sub(now(), interval 10 day)       group         ia.areaid     ) g on a.id = g.areaid   a.cityid = '1'  order    a.name asc ; 

or using inline subquery:

select   a.id, a.name,    coalesce(        ( select            count(*)                      item_area ia            join              item on  i.id = ia.itemid                    i.published > date_sub(now(), interval 10 day)           ,           a.id = ia.areaid       ), 0           ) recentitems      area   a.cityid = '1'  order    a.name asc ; 

Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -