SQL多表查詢

MS SQL三表查詢:測試

select Inventory.cInvCode,Inventory.cInvName,max(dDate) from Inventory,SaleBillVouchs,SaleBillVouch
where Inventory.cInvCode = SaleBillVouchs.cInvCode
and SaleBillVouchs.SBVID = SaleBillVouch.SBVID
group by Inventory.cInvCode,Inventory.cInvName
order by Inventory.cInvCode  

 

select Inventory.cInvCode as '存貨編碼',Inventory.cInvName as '存貨名稱',convert(char(10),max(dDate),120) as '最後出貨日期' from Inventory 
left join SaleBillVouchs
on Inventory.cInvCode = SaleBillVouchs.cInvCode 
left join SaleBillVouch
on SaleBillVouchs.SBVID = SaleBillVouch.SBVID
group by Inventory.cInvCode,Inventory.cInvName
order by Inventory.cInvCode

 

select InventoryClass.cInvCName as '存貨分類',
       Inventory.cInvCode as '存貨編碼',
       Inventory.cInvName as '存貨名稱',
       CurrentStock.iquantity as '結存數量',
       CurrentStock.foutquantity as '待發貨數量',
       CurrentStock.iquantity -CurrentStock.foutquantity as '可用量 ',
       AA_BatchProperty.cBatchProperty1 as '進貨價格',
       AA_BatchProperty.cBatchProperty1 * (CurrentStock.iquantity -CurrentStock.foutquantity) as '採購金額',
       convert(char(10),max(dDate),120) as '最後出貨日期',
       datediff(day,convert(char(10),max(dDate),120),getdate()) as '呆滯天數'
from Inventory 
left join SaleBillVouchs
on Inventory.cInvCode = SaleBillVouchs.cInvCode 
left join SaleBillVouch
on SaleBillVouchs.SBVID = SaleBillVouch.SBVID
left join CurrentStock 
on Inventory.cInvCode = CurrentStock.cInvCode
left join InventoryClass
on Inventory.cInvCCode = InventoryClass.cInvCCode
left join AA_BatchProperty
on CurrentStock.cBatch = AA_BatchProperty.cBatch and  CurrentStock.cInvCode = AA_BatchProperty.cInvCode
where CurrentStock.iquantity != 0 and InventoryClass.cInvCName != 'MTK'
group by Inventory.cInvCode,Inventory.cInvName,Inventory.cInvCCode,CurrentStock.iquantity,CurrentStock.foutquantity, InventoryClass.cInvCName,AA_BatchProperty.cBatchProperty1
order by Inventory.cInvCode

 

LEFT JOIN 關鍵字會從左表 (table_name1) 那裏返回全部的行,即便在右表 (table_name2) 中沒有匹配的行。 編碼

RIGHT JOIN 關鍵字會右表 (table_name2) 那裏返回全部的行,即便在左表 (table_name1) 中沒有匹配的行。spa

 

MySQL:未測試code

SELECT * FROM tx1 left join (tx2, tx3) ON (tx1.id=tx2.tid AND tx2.tid=tx3.tid) where tx1.id = 3

 

獲取SQL Server日期而不包括時間的方法htm

http://database.51cto.com/art/201009/228086.htmblog

相關文章
相關標籤/搜索