gpt4 book ai didi

mysql - 如何选择表和统计其他表

转载 作者:行者123 更新时间:2023-11-29 02:03:07 25 4
gpt4 key购买 nike

假设我有两个表:

客户

  Name   |   id   |
-------------------
Benny | 1 |
Wilson | 2 |
Joe | 3 |
Austin | 4 |

订单

  Product  |   id   |
---------------------
TV | 1 |
Hifi-set | 1 |
HTPC | 1 |
CD | 1 |
DVD | 1 |
CD | 1 |
DVD | 1 |

这就是我想要的结果:

  Name   | Orders |
-------------------
Benny | 7 |
Wilson | 0 |
Joe | 0 |
Austin | 0 |

我不熟悉SQL,但我试过:

SELECT c.Name FROM Customers AS c LEFT JOIN Orders AS o ON c.id=o.id GROUP BY c.Name

但是得到了一个错误的结果:

  Name   | Orders |
-------------------
Benny | 4 |
Wilson | 1 |
Joe | 1 |
Austin | 1 |

我该怎么办?

最佳答案

尝试:

select 
c.Name,
(select count(1) from Orders where ID=c.ID)
from
Customers as c

关于mysql - 如何选择表和统计其他表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11443124/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com