我在 mysql php 中有一个问题请帮忙
我有下表(用户):
id name
------------
1 test
2 test2
3 test3
4 test4
5 test5
另一个名为 (qty) 的表:
id user_id available
-----------------------
1 1 1
2 1 1
3 2 0
4 2 1
5 3 0
6 4 0
我需要计算有多少可用 (1) 并显示不可用的:
输出如下:
user_name count_available
----------------------------
test 2
test2 1
test3 0
test4 0
test5 0
这可以通过一条 SQL 语句完成吗?如何实现?
谢谢
最佳答案
这是一个聚合和左连接:
select u.name, sum(q.available) as count_available
from users u left join
qty q
on u.id = q.user_id
group by u.name;
关于php - 统计mysql中可用的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21128506/