我有这两个表:
table1:stock_list
Stock_name Year Qty Reserve
stock1 2015 1 0
stock1 2016 1 1
stock1 2016 1 0
stock1 2016 1 0
stock2 2015 1 0
stock2 2015 1 0
stock2 2015 1 0
stock2 2016 1 1
stock2 2016 1 1
stock2 2016 1 0
第二个表是:产品
Variant MY List_Price
stock1 2015 2000
stock1 2016 0
stock2 2015 3000
stock2 2016 NULL
stock3 2016 4000
我要找的结果是这张表:
Name Year Qty Reserved List_Price
stock1 2016 3 1 0
stock2 2016 3 2 Null
有没有办法通过一个MySQL语句来达到这个结果。
答案 0 :(得分:0)
尝试:
Select S.Stock_Name as Name, S.Year, sum(S.Qty) as Qty, sum(S.Reserved) as Reserve, P.List_Price
from stock_list S inner join products P
on S.Stock_name=P.Variant and S.Year=P.MY
where P.List_Price=0 or P.List_Price is null
group by S.Stock_Name, S.Year, P.List_Price
答案 1 :(得分:0)
尝试此查询:
SELECT sl.stock_name,
sl.year,
sum(sl.qty) AS qty,
sl.reserve
FROM stock_list AS sl
JOIN products AS p ON sl.stock_name=p.variant
WHERE p.listprice IS NULL
OR p.listprice=0
GROUP BY sl.stock_name