gpt4 book ai didi

sql - MySQL 多表计数查询

转载 作者:行者123 更新时间:2023-11-29 05:43:52 27 4
gpt4 key购买 nike

我有3张 table

products
productid (int)
name (varchar)
price (float)

sales
salesid (int)
productid (int)
time (datetime)

links
linkid (int)
productid (int)
link (text)

现在我需要一个可以显示为的查询

ProductID      ProductName      Sales      Links
1 ABC 10 8
2 XYZ 15 12

我需要特定产品的所有销售计数和所有链接计数

我怎样才能做到这一点?

谢谢

最佳答案

SELECT p.productid, p.pname AS ProductName, 
Count(DISTINCT s.salesid) as Sales, Count(DISTINCT l.linkid) as Links
FROM products p
LEFT JOIN sales s ON p.productid=s.productid
LEFT JOIN links l ON p.products=l.productid
GROUP BY p.productid

关于sql - MySQL 多表计数查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4408393/

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