Two different fields in one table are associated with the same field in another table

Table A

ID NAME_ID1 NAME_ID2 ClASS
1 956 966 Class 11

Table B

ID NAME
956 Xiao Zhou
966 Xiao Rui

effect

ID NAME1 NAME2 ClASS
1 Xiao Zhou Xiao Rui Class 11

Want this effect?

SELECT
	a.ID,
	b. NAME AS NAME1,
	c. NAME AS NAME2,
	a.ClASS
FROM
	A a
LEFT JOIN B b ON a.NAME_ID1 = b.ID
LEFT JOIN C c ON a.NAME_ID2 = b.ID

This requires us to query the same table twice associating LEFT JOIN.

There is a better way, please leave a message to me

Guess you like

Origin blog.csdn.net/weixin_43957211/article/details/110441169