以下数据库:
Table MatA:
ID NomCom_ID ProFo_ID
1 1 1
2 2 2
Table MatB:
ID NomCom_ID ProFo_ID
1 1 2
2 2 2
Table NomCom:
ID Val
1 Steel
2 Copper
Table ProFo:
ID Val
1 Sheet
2 Pipe
通过以下查询,我得到带有链接输入的表MatA:
SELECT M.ID,
NomCom.Val AS NomCom,
ProFo.Val AS ProFo
FROM MatA M
LEFT JOIN NomCom ON M.NomCom_ID = NomCom.ID
LEFT JOIN ProFo ON M.ProFo_ID = ProFo.ID
;
结果:
Table MatA:
ID NomCom ProFo
1 Steel Sheet
2 Copper Pipe
通过以下查询,我仅获得表MatA AND MatB中可用的输入:
SELECT A.* FROM MatA A
INNER JOIN MatB B
ON A.NomCom_ID = B.NomCom_ID
AND A.ProFo_ID = B.ProFo_ID
;
结果:
Table MatA:
ID NomCom_ID ProFo_ID
2 2 2
如何获得此结果的正确语法,但是具有来自子表的链接项,如下所示:
Table MatA:
ID NomCom ProFo
2 Copper Pipe
感谢您的帮助
答案 0 :(得分:0)
如果只想获取这些ID的详细信息,则只需从ID加入这些表并获取value字段。
SELECT A.ID, NomCom.Val, ProFo.Val FROM MatA A
INNER JOIN MatB B
ON A.NomCom_ID = B.NomCom_ID AND A.ProFo_ID = B.ProFo_ID
LEFT JOIN NomCom AS n ON A.NomCom_ID = n.NomCom.ID
LEFT JOIN ProFo AS p ON A.ProFo_ID = p.ProFo.ID
它将进行内部联接以获得以下结果:
Table MatA:
ID NomCom_ID ProFo_ID
2 2 2
然后,额外的联接将为您获取值并返回此值:
Table MatA:
ID NomCom ProFo
2 Copper Pipe