I have 3 tables
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)
I need output as such
ProductID Name TotalSales TotalLinkAvailable
1 ABCD 10 12
1 EFGH 7 25
How can I achieve this using single query?
Thanks
EDIT
I have tried 开发者_如何转开发the following query which did not work:
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
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
Assuming that there is a typo regarding the ProductID in the second row of your exemplary query result.
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
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
精彩评论