我正在寻求查询的帮助,该查询显示特定商品的库存商品的移动。
表1名称:saleitems字段: itemid ,saledate,qty
表2名称:purchaseitems字段: itemid ,purchaseate,qty
表3名称:stocktransfers字段: itemid ,trfdate,from,to,qty
我想从所有三个表中提取mysql数据,并根据日期字段对它们进行排序。非常感谢任何帮助。
预期输出应如下
答案 0 :(得分:3)
select * from saleitems,purchaseitems,stocktransfers
where your condition order by saledate,purchasedate,trfdate
您可以使用order by子句根据条件选择您想要的任何字段,或按照您的逻辑加入
答案 1 :(得分:2)
试试这个:
(select itemid, "SALES" as type, saledate as dt, qty from salesitems)
union
(select itemid, "PURCHASE" as type, purchasedate as dt, qty from purchaseitems)
union
(select itemid, "STOCKTRANSFER" as type, trfdate as dt, qty from stocktransferitems)
order by dt;
我希望,我的问题是对的。
答案 2 :(得分:0)
希望这有帮助
select * from salesitems a
inner join purchaseitems b on a.itemid=b.itemid
inner join stocktransfer c on a.item_id=c.item_id
order by a.saledate,b.purchasedate,c.trfdate
答案 3 :(得分:0)
SELECT a.itemid,a.saledate,a.qty,b.purchasedate,b.qty,c.trfdate,c.from,c.to,c.qty
FROM saleitems a INNER JOIN purchaseitems b
ON a.itemid = b.itemid
INNER JOIN stocktransfers c
ON a.itemid = b.itemid
WHERE a.itemid = 10
ORDER BY a.saledate , b.purchasedate , c.trfdate