MySQL 从连接表中计算两个组
我有一个名为 Tickets 的表,其中包含 id、name。 以及一个名为 Articles 的表,其中包含 id、ticket_id 和 status。
我正在尝试显示所有票证名称,以及带有该 Ticket_id 的文章计数,以及带有该 Ticket_id 和 status = '1' 的文章计数
到目前为止,我已经尝试过此操作,但没有成功:
SELECT t.name, count(a.id), count(aa.id)
FROM tickets t
LEFT JOIN articles a ON t.id = a.ticket_id GROUP BY t.id
LEFT JOIN articles aa ON t.id = aa.ticket_id AND aa.status = '1' GROUP BY t.id
提前感谢任何帮助和建议。
I have a table called Tickets with id, name.
and a table called Articles with id, ticket_id, and status.
I'm trying to display all Ticket names, along with a COUNT of Articles with that ticket_id, and also a COUNT of articles with that ticket_id and status = '1'
So far I've tried this with no success:
SELECT t.name, count(a.id), count(aa.id)
FROM tickets t
LEFT JOIN articles a ON t.id = a.ticket_id GROUP BY t.id
LEFT JOIN articles aa ON t.id = aa.ticket_id AND aa.status = '1' GROUP BY t.id
Thanks in advance for any help and advice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它应该有效...
It should work ...