mysql - GROUP BY and ORDER BY issues -
i have following query:
select distinct ( s.styletitle ), count(p.id) `picturecount` `style` s left join `instagram_picture_style` ps on s.id = ps.style_id left join `instagram_shop_picture` p on ps.picture_id = p.id left join `instagram_picture_category` c on c.picture_id = p.id left join `instagram_second_level_category` sl on c.second_level_category_id = sl.id sl.id =25 group p.id order picturecount
however query gives me:
i wanted list ordered style has pictures in it. did wrong? why giving me 1 on of styles, pretty sure has more pictures style
order by
doesn't have underscores. equally important, using distinct
in way seem think function. not. modifies on select
, applies columns.
you should group by
same column have in distinct. this:
select s.styletitle, count(p.id) `picturecount` `style` s left join `instagram_picture_style` ps on s.id = ps.style_id left join `instagram_shop_picture` p on ps.picture_id = p.id left join `instagram_picture_category` c on c.picture_id = p.id left join `instagram_second_level_category` sl on c.second_level_category_id = sl.id sl.id = 25 group s.styletitle order picturecount desc;
in fact, never need distinct
group by
. if using, need think why necessary.
Comments
Post a Comment