0

我有一张桌子,看起来像这样,

mysql> select * from match_info;
+---------------+---------+------------+-------------+-----------+
| match_info_id | user_id | body_types | hair_colors | ethnicity |
+---------------+---------+------------+-------------+-----------+
|             1 |       1 | 1,5,9,13   | 1,5,9,13    | 2,6,10    |
|             2 |       2 | 1,5,9,13   | 5           | 1,5,9     |
+---------------+---------+------------+-------------+-----------+

我在每个表中都使用了body_typeshair_colorsethnicity和列的查找表idname

使用上面我需要为特定用户选择所有值。像这样。

来自 body_type 表。

体型:Skinny, Muscular, Large, Ripped

发色:Blonde, Dark Brown, Strawberry Blonde, Dark Blonde等等……

谁能告诉我如何进行选择查询以获得上述结果。

希望有人可以帮助我。

谢谢你。

4

1 回答 1

0

您可以加入表并将find_in_set函数放在 on 子句中。尝试这个 :

select t.match_info_id, t.user_id,
    group_concat(distinct a.name) as body_types,
    group_concat(distinct b.name) as hair_colors,
    group_concat(distinct c.name) as ethnicity
from match_info as t
    inner join body_type as a on find_in_set(a.id,t.body_types)
    inner join hair_color as b on find_in_set(b.id,t.hair_colors)
    inner join ethnicity as c on find_in_set(c.id,t.ethnicity)
group by t.match_info_id, t.user_id

请注意,您可以根据需要使用left join而不是使用。inner join

于 2015-06-07T03:21:07.640 回答