有以現兩張表:
規格表A有字段code,spec,unit
BOM表B有三個字段parent,child,qty
現在要列出BOM表的父料號的規格,子料號的規格
我用這樣的SQL語句
select parent,parentspec,parentunit,child,spec as childspec,unit as childunit
from
(select parent,spec as parentspec,unit as parentunit,child from A,B where B.parent=A.code) T1,A
where T1.child=A.code
感覺這樣的SQL語句比較繁鎖,有沒有可以優化的地方?
------解决方案--------------------
这样带subquery的SQL还是比较正常的,
如果不知道数据量大下,索引,执行计划等细节,很难对某个SQL进行具体优化。
------解决方案--------------------
不做子查詢直接關聯
SELECT
A.parent,
B.spec AS parentspec,
B.unit AS parentunit,
A.child,
C.spec as childspec,
C.unit as childunit
FROM
BOM A
INNER JOIN
XXX AS B
ON A.parent = B.code
INNER JOIN
XXX AS C
ON A.child = C.code