2012-07-11 110 views
-1

我如何加入两个表的问题,我有以下表SQL连接两张表

ITEM_TAB 
-------------------------------------------------- 
ItemID, Qty, Price, EleCode, WomCode, MenCode 
-------------------------------------------------- 

CODES_TAB 
-------------------------------------------------- 
CODE  | TYPE    |  DESCRIPTION 
-------------------------------------------------- 
AA  |  ELECTRONICS |   ... 
AA  | WOMEN   |   ... 
AA  | MENS    |   ... 
BB  | GROCERY   |   .... 
BB  |  DELI   | 
-------------------------------------------------- 

项目表只包含代码(EleCode是TYPE电子,WomCode是TYPE妇女等) ,并且CODES是查找表。这两个人没有任何关联。在单个查询中,我需要像下面那样检索。

ItemID,Qty,Price,EleCode,Desction,WomCode,Description,MenCode,Description。

select i.itemId 
    , i.Qty 
    , i.price 
    , i.EleCode 
    , (select description from code where code='AA' and TYPE='ELECTRONICS') 
    , i.WomCode 
    , (select description from code where code='AA' and TYPE='WOMEN') 
    , i.MenCode 
    , (select description from code where code='AA' and TYPE='MENS') 
from ITEM 

能否请你帮我在制定此查询。

+0

是您关于MySQL或Oracle的问题吗?这两个标签都不属于那里,除非你实际使用两个DBMS。如果您因Java而添加了'oracle'标记,请将其删除;这就像使用microsoft作为windows7问题的标签 - 制造商或商标所有者无关紧要,因为您知道谁拥有/发布了windows7。 (其实,为什么这个标签为'java'?这里没有提及任何编程语言。) – 2012-07-11 01:33:45

回答

0
select ItemID, Qty, Price, EleCode, e.description, womcode , w.description, mencode, m.description 
from item_tab 
left join codes_tab e 
on (eleCode = e.code) 
left join codes_tab w 
on (womcode = w.code) 
left join codes_tab m 
on (mencode = m.code); 
0

我的某个人对此架构有很大的帮助吗?

喜欢的东西

Select ItemId,Qty,Price,Elecode,e.description,womcode,w.description,mencode,m.description 
left join codes_tab e On e.Type='ELECTRICAL' and e.code = elecode 
left join codes_tab w On w.Type='WOMENS' and w.code = womcode 
left join codes_tab m On m.Type='MENS' and m.code = mencode