我从四个不同的表中获取数据。这就是我的方式:
select value_split.id,
count(invoice_request.id) as counts,
GROUP_CONCAT(`category`.`category_name`) as `category_name`
from value_split
left join invoice_request
on invoice_request.id = value_split.id
and MONTH(invoice_request.from_date) = '05'
and YEAR(invoice_request.from_date) = '2016'
and invoice_request.status = '1'
left join `category_value` on value_split.id = category_value.id
left join `category` on category_value.category_id = category.category_id
where MONTH(value_split.date) = '05'
and YEAR(value_split.date) = '2016'
group by value_split.id, value_split.date
limit 0, 2147483647
预期的答案是:
id|counts|category_name
1 | 1 | service, drop
2 | 2 | drop
3 | 1 | service
但是,我得到的是:
id|counts|category_name
1 | 5 | service, drop
2 | 4 | drop
3 | 5 | service
如果我删除第10行和第11行,我会得到正确答案。但是,如果我加入它们,我会得到错误的。但是,我也希望展示它。那么,我该怎么做才能获得正确的输出?这有什么问题?
答案 0 :(得分:0)
使用
COUNT(DISTINCT invoice_request.id) AS counts,