sql - MySql 5.7 ORDER BY clause is not in GROUP BY clause and contains nonaggregated column -
i'm trying figure out without disabling "only_full_group_by" in my.ini
here query:
select p.title, count(t.qty) total payments t left join products p on p.id = t.item t.user = 1 group t.item order t.created desc;
and tables:
payments:
id item user created ============================ 1 1 1 2017-01-10 2 2 1 2017-01-11 3 3 1 2017-01-12 4 4 1 2017-01-13 5 1 1 2017-01-14
products:
id title created ========================== 1 first 2016-12-10 1 second 2016-12-11 1 third 2016-12-12 1 fourth 2016-12-13
the final result should lie:
name total first 2 second 1 third 1 fourth 1
but if change query group t.item, t.created
error gone, end 5 records instead of four, not want. since i'm grouping items based on "item" field, there should 4 records
this query:
select p.title, count(t.qty) total -------^ payments t left join products p on p.id = t.item t.user = 1 group t.item ---------^ order t.created desc; ---------^
the pointed places have issues. notice select
, group by
referring different column. in left join
, (pretty much) want aggregate in first table, not second.
the order by
problem. not aggregating column, need decide value want. guessing min()
or max()
:
select p.title, count(t.qty) total payments t left join products p on p.id = t.item t.user = 1 group p.title order max(t.created) desc;
i add count(t.qty)
suspect. qty
refers "quantity" , want sum: sum(t.qty)
.
Comments
Post a Comment