只能关联啊,可以写成view,下次查询的时候直接查view,不用每次都去关联这么多表!
只能多表联合查询,然后创建视图,这样为其他提供便利,减少重复sql语句
如下:
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
不清楚你要进行哪方面的优化,主要有两方面的优化:1业务 2SQL本身
业务 要求对使用这种查询进行逻辑分析 表结构修改 等等
SQL 建议创建索引 缩小查询子集等方法