SQL内连接具有相同键的3个表

时间:2018-04-13 19:22:57

标签: sql inner-join

我想尝试将3个表连接在一起,它们都使用名为ITEM_NO项号的相同密钥。我可能实际上需要再添加一个表,也称为#34; USR_MAG_ITEM_EXP"这也是一个内部联接吗?

表格为dbo.IM_PRCdbo.IM_ITEMdbo.IM_INV_CELL抱歉是菜鸟!

SELECT     dbo.IM_PRC.ITEM_NO, dbo.IM_PRC.LOC_ID, dbo.IM_PRC.DIM_1_UPR, 
dbo.IM_PRC.DIM_2_UPR,dbo.IM_PRC.DIM_3_UPR, dbo.IM_PRC.REG_PRC,dbo.IM_PRC.PRC_1, dbo.IM_PRC.PRC_2, dbo.IM_ITEM.CATEG_COD, dbo.IM_ITEM.SUBCAT_COD, dbo.IM_ITEM.STAT, dbo.IM_ITEM.ITEM_VEND_NO, dbo.IM_ITEM.IS_ECOMM_ITEM, dbo.IM_ITEM.VEND_ITEM_NO,dbo.IM_ITEM.LST_COST,dbo.IM_ITEM.USER_MAG_NAME,  dbo.IM_INV_CELL.LOC_ID,dbo.IM_INV_CELL.DIM_1_UPR, dbo.IM_INV_CELL.DIM_2_UPR, dbo.IM_INV_CELL.DIM_3_UPR, dbo.IM_INV_CELL.MIN_QTY,dbo.IM_INV_CELL.MAX_QTY, dbo.IM_INV_CELL.QTY_ON_HND,   


 FROM dbo.IM_PRC 
 INNER JOIN dbo.IM_ITEM ON dbo.IM_PRC.ITEM_NO = dbo.IM_ITEM.ITEM_NO 
 INNER JOIN dbo.IM_INV_CELL ON dbo.IM_INV_CELL.ITEM_NO = dbo.IM_PRC.ITEM_NO


    WHERE     (dbo.IM_ITEM.ITEM_VEND_NO = 'tum')

感谢!!!

4 个答案:

答案 0 :(得分:2)

您必须为每个单独的表重复JOIN

SELECT *
FROM dbo.IM_PRC 
INNER JOIN dbo.IM_ITEM ON dbo.IM_PRC.ITEM_NO = dbo.IM_ITEM.ITEM_NO 
INNER JOIN dbo.IM_INV_CELL ON dbo.IM_INV_CELL.ITEM_NO = dbo.IM_PRC.ITEM_NO
WHERE     (dbo.IM_ITEM.ITEM_VEND_NO = 'X')

我建议使用别名并删除不必要的括号:

SELECT *
FROM dbo.IM_PRC P
INNER JOIN dbo.IM_ITEM I ON P.ITEM_NO = I.ITEM_NO 
INNER JOIN dbo.IM_INV_CELL IC ON IC.ITEM_NO = P.ITEM_NO
WHERE I.ITEM_VEND_NO = 'X'

答案 1 :(得分:1)

Invoke-WebRequest -Uri $Url -Method POST -Body $body -Headers @{ 'Content-Type' = 'application/json'; 'Authorization' = "Bearer " + $API_key }

如果您需要来自任何一个表而不是INNER的所有详细信息,则可以使用LEFT或RIGHT联接。

答案 2 :(得分:0)

您应该明确加入3表

SELECT *
FROM dbo.IM_PRC 
INNER JOIN dbo.IM_ITEM ON dbo.IM_PRC.ITEM_NO = dbo.IM_ITEM.ITEM_NO 
INNER JOIN dbo.IM_INV_CELL ON  dbo.IM_INV_CELL.ITEM_NO = dbo.IM_PRC.ITEM_NO
     AND  dbodbo.IM_INV_CELL.M_VEND_NO = 'X'

答案 3 :(得分:0)

见下文

Select * 
From dbo.IM_PRC PRC
Inner join dbo.IM_ITEM ITEM ON PRC. ITEM_NO = ITEM. ITEM_NO
Inner join dbo.IM_INV_CELL CELL ON PRC. ITEM_NO = CELL. ITEM_NO
WHERE ITEM.ITEM_VEND_NO = 'X'