我有3张桌子
products [which contains data related to products]
productid (int)
name (varchar)
price (float)
sales [which contains data related to sales]
salesid (int)
productid (int)
time (datetime)
links [which contains data related to links from products]
linkid (int)
productid (int)
link (text)
我需要输出
ProductID Name TotalSales TotalLinkAvailable
1 ABCD 10 12
1 EFGH 7 25
如何使用单个查询实现此目的?
由于
修改
我尝试了以下不起作用的查询:
select p.name,count(s.salesid) as Sales, count(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
答案 0 :(得分:0)
select p.productid, p.name, count(s.salesid) as TotalSales, count(l.linkid) as TotalLinkAvailable
from products p
left join sales s
on p.productid = s.productid
left join links l
on p.productid = l.productid
group by p.productid, p.name
答案 1 :(得分:0)
select p.pname,count(s.salesid) as Sales ,count(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
答案 2 :(得分:0)
SELECT p.productid AS ProductID, p.name AS Name, COUNT(s.salesid) AS TotalSales, COUNT(l.linkid) AS TotalLinkAvailable
FROM products AS p
LEFT JOIN sales AS s ON s.productid = p.productid
LEFT JOIN links AS l ON l.productid = p.productid
GROUP BY p.productid, p.name
假设在示例性查询结果的第二行中有关于ProductID的拼写错误。