-1

我有一个关于如何加入两个表的问题,我有以下表

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 Electronics,WomCode 属于 TYPE Womens 等),CODES 是查找表。这两者没有任何关联。在单个查询中,我需要如下检索。

ItemID、数量、价格、EleCode、Desction、WomCode、描述、MenCode、描述。

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

你能帮我制定这个查询吗?

4

2 回答 2

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);
于 2012-07-11T01:32:05.997 回答
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
于 2012-07-11T01:40:25.703 回答