我有3個表3臺數查詢mysql的
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
使用'JOIN','GROUP BY'和'COUNT'。我只是懶得寫現在的查詢。 – Bobby 2010-12-10 12:58:41
@Bobby:我試過以下查詢select p.name,count(s.salesid)as Sales,count(l.linkid)as鏈接 from產品p left join sales s on p.productid = s.productid left join links l on p.products = l.productid group by p.productid,但它不起作用 – 2010-12-10 12:59:55
請務必在問題中告訴我們您已經嘗試了什麼,它可以幫助您儘可能多地幫助您。 – Bobby 2010-12-10 13:13:37