好的,伙计们,不好意思问这个问题,因为我看过几个mysql JOIN示例,但是我似乎无法使它正常工作。
“销售”
----------------------
idcustomer | datecode
----------------------
1 | 20120503
1 | 20120503
1 | 20120503
2 | 20120503
3 | 20120503
我想知道谁是最大的买家。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。做:
SELECT idcustomer, COUNT(idcustomer) FROM sales WHERE datecode = 20120503 GROUP BY idcustomer ORDER BY COUNT(idcustomer) DESC
我得到:
-----------------------------
idcustomer | Count(idcustomer)
-----------------------------
1 | 3
2 | 1
3 | 1
问题是...因为我也有桌子:
“顾客”
----------------------
| name | id_customer |
----------------------
Jess | 1
Matt | 2
Perry | 3
以下是我想实现的目标。
---------------------------------------------
customer.name | idcustomer | Count(idcustomer)
---------------------------------------------
Jess | 1 | 3
Matt | 2 | 1
Perry | 3 | 1
最佳答案
SELECT customer.name, idcustomer, COUNT(idcustomer)
FROM sales
JOIN customer
ON sales.idcustomer = customer.id_customer
WHERE datecode = 20120503
GROUP BY idcustomer
ORDER BY COUNT(idcustomer) DESC
看到它在线运行:sqlfiddle
关于mysql - mysql + JOIN查询问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10908680/