mysql一个字段多个值关联另一张表

时间:2021-12-05 14:59:27

    A表的id字段是以'1,2,3,4,5'...这样的形式存的,它关联字典表B的id字段,此时需求是要查出name

  SQL:

SELECT
	a.id,
	(
		SELECT
			GROUP_CONCAT(t.NAME)
		FROM
			test t
		WHERE
			FIND_IN_SET(
				t.id,
				a.fixedAssetClass
			)
	)name
FROM
	table a
WHERE
	a.id = '123'


还有一种情况会用到循环。

SQL:
SELECT 
a.* 
FROM table a
........
where a.id in 
<foreach collection="arr" item="item" index="index" 
open="(" separator="," close=")">#{item}</foreach>
GROUP BY a.id