MYSQL全关联的实现思路

MYSQL全关联的实现思路,第1张

4张表依次使用union

SELECT E.NAME ,E.COLUMN1,E.COLUMN2,E.COLUMN3,E.COLUMN4 FROM (

SELCT NAME FROM A

UNION

SELCT NAME FROM B

UNION

SELCT NAME FROM C

UNION

SELCT NAME FROM D) E

GROUP BY E.NAME

SELECT

A.Name,

A.COLUMN1,

B.COLUMN2,

C.COLUMN3,

D.COLUMN4

FROM

A LEFT JOIN B

ON ( A.NAME = B.NAME )

LEFT JOIN C

ON ( A.NAME = C.NAME )

LEFT JOIN D

ON ( A.NAME = D.NAME )

UNION

SELECT

B.Name,

A.COLUMN1,

B.COLUMN2,

C.COLUMN3,

D.COLUMN4

FROM

B LEFT JOIN A

ON ( A.NAME = B.NAME )

LEFT JOIN C

ON ( B.NAME = C.NAME )

LEFT JOIN D

ON ( B.NAME = D.NAME )

UNION

SELECT

C.Name,

A.COLUMN1,

B.COLUMN2,

C.COLUMN3,

D.COLUMN4

FROM

C LEFT JOIN A

ON ( C.NAME = A.NAME )

LEFT JOIN B

ON ( B.NAME = C.NAME )

LEFT JOIN D

ON ( C.NAME = D.NAME )

UNION

SELECT

D.Name,

A.COLUMN1,

B.COLUMN2,

C.COLUMN3,

D.COLUMN4

FROM

D LEFT JOIN A

ON ( D.NAME = A.NAME )

LEFT JOIN B

ON ( B.NAME = D.NAME )

LEFT JOIN C

ON ( C.NAME = D.NAME )


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/8408573.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-16
下一篇 2023-04-16

发表评论

登录后才能评论

评论列表(0条)

保存