MySQL查询从多个表中获取数据 [英] MySQL Query to fetch data from multiple tables
本文介绍了MySQL查询从多个表中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下表格
1)产品(产品编号,名称,描述,价格)
2)销售(销售编号,产品编号,买方名称,买方电子邮件,状态)
3)ProductViews(viewid,productid)
I have following tables
1) Products (productid, name, description, price)
2) Sales (salesid, productid, buyername, buyeremail, status)
3) ProductViews (viewid, productid)
现在,我需要一个可以输出为
的查询
产品名称 价格 b&bsp销售总额
Now, I need a query that can output as
ProductID ProductName Price TotalViews TotalSales
感谢您的帮助,谢谢.
推荐答案
SELECT p.productid, p.name, p.price, COUNT(pv.viewid) AS totalviews, COUNT(s.salesid) AS totalsales
FROM Products p
LEFT JOIN Sales s ON s.productid = p.productid
LEFT JOIN ProductViews pv ON pv.productid = p.productid
GROUP BY p.productid, p.name, p.price
出于完整性考虑,扩展了分组依据,但可能只是p.productid.
Extended group by for completeness sake but it could just be p.productid.
这篇关于MySQL查询从多个表中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文