数据库多表关联查询
2016-07-10 来自:cnjc 3 人回应

如果在查询数据时关联了10几张表的话,有什么好的方法解决。

2016-07-25 来自:BAD鼻血

如下:

CREATE VIEW V_FinanceINo(ID,InventoryNo,InventoryTitle,InventoryType,TotalMoney,IsState,InventoryMark)
AS SELECT ID,InventoryNo,InventoryTitle,InventoryType,TotalMoney,IsState,InventoryMark FROM (
    SELECT ID,inv.InventoryNo,inv.InventoryTitle,inv.InventoryType,
    (SELECT SUM(Number*Price) FROM dbo.InventoryDetail WHERE InventoryID=inv.ID GROUP BY InventoryID)TotalMoney ,
    IsState,InventoryMark FROM dbo.Inventory inv WHERE InventoryNo NOT IN (SELECT OrderNo FROM dbo.FinanceIDetails)
    UNION ALL    
    SELECT ID,PurchaseNo,PurchaseTitle,
    '采购进货' InventoryType,
    (SELECT SUM(Number* Price) FROM dbo.PurchaseDetails WHERE PurchaserID=dbo.Purchase.ID GROUP BY PurchaserID)TotalMoney,
    IsState,PurchaseMarker FROM dbo.Purchase WHERE PurchaseNo NOT IN (SELECT OrderNo FROM dbo.FinanceIDetails)
  ) T

2016-07-21 来自:Mr. Lee

只能关联啊,可以写成view,下次查询的时候直接查view,不用每次都去关联这么多表!

2016-07-20 来自:Isabelle

只能多表联合查询,然后创建视图,这样为其他提供便利,减少重复sql语句

您的回应

你还未登陆,不能回应!登陆