MYSQL:查询输出所有相关/标签/类似的ID

时间:2013-04-09 12:18:35

标签: mysql sql

我正在处理一个查询,但是无法让它按照我的意愿行事。

我有这张桌子:家庭

family.id
family.name
family.related

有了这个内容:

id        | name        | related
1         | Name1       | 
2         | Name2       | 3,5
3         | Name3       | 
4         | Name4       | 
5         | Name5       | 4,1

正如你所看到的,id 2有相关的(ids)3,5但是如果你去(id)5你会得到另一个相关的(ids)4,1

所以当我选择family.id 2我想输出:3,5,4,1

这可以通过1次查询吗?

以下查询带我到3,5:

SELECT

GROUP_CONCAT(related1.related) AS related_ids,

family.id AS id,
family.name AS name,
family.related AS related

FROM family family

LEFT JOIN family related1
              ON FIND_IN_SET(family.id, related1.related) >0

WHERE family.id=5

我想让related_ids包含3,5,4,1

0 个答案:

没有答案