表名= 产品
--------------------- ID Product Name --------------------- 1 Samsung 2 iPhone 3 HTC
表名= 卖
----------------------------------------------------- ID Product_id Price($) Date(d/m/y) ----------------------------------------------------- 1 1 400 11/11/2017 2 2 500 11/11/2017 3 3 450 11/11/2017 4 2 500 11/11/2017 5 2 500 11/11/2017 6 1 400 11/11/2017 7 1 400 11/11/2017 8 3 450 11/11/2017 9 1 400 12/11/2017 10 2 500 12/11/2017 11 3 450 12/11/2017 12 2 500 12/11/2017 13 2 500 12/11/2017 14 1 400 12/11/2017 15 1 400 12/11/2017 16 3 450 12/11/2017 17 1 400 13/11/2017 18 2 500 13/11/2017 19 3 450 13/11/2017 20 2 500 13/11/2017 21 2 500 13/11/2017 22 1 400 13/11/2017 23 1 400 13/11/2017 24 3 450 13/11/2017
我想在浏览器上显示如下表:
------------------------------------------------------------------------------- Ser No Product Name Date Total Product Total Price($) ------------------------------------------------------------------------------- 1 Samsung 11/11/2017 3 1200 2 iPhone 11/11/2017 3 1500 3 HTC 11/11/2017 2 900 4 Samsung 12/11/2017 3 1200 5 iPhone 12/11/2017 3 1500 6 HTC 12/11/2017 2 900 7 Samsung 13/11/2017 3 1200 8 iPhone 13/11/2017 3 1500 9 HTC 13/11/2017 2 900
答案 0 :(得分:0)
除了第一栏之外,这样的事情会起作用:
SELECT
Product.Name,
Sell.DateSell,
Count(ProductId) as total,
Sum(Sell.Price) as total_price,
FROM Product
INNER JOIN Sell on Sell.Product_id = Product.ID
GROUP BY Product.Name, Sell.DateSell
如果你想添加一个像'Ser No'这样的行号:
SELECT
@rownum := @rownum + 1 AS 'Ser No',
t.Name as 'Product Name',
t.DateSell as 'Date',
t.total as 'Total Product',
t.total_price as 'Total Price'
FROM
( SELECT
Product.Name,
Sell.DateSell,
Count(ProductId) as total,
Sum(Sell.Price) as total_price,
FROM Product
INNER JOIN Sell on Sell.Product_id = Product.ID
GROUP BY Product.Name, Sell.DateSell
) t, (SELECT @rownum := 0) r