mysql-拆分列表,以逗号分隔为INT列表

时间:2018-06-24 08:00:33

标签: mysql sql

question:

+-----------+-------------------------+
|question id|          help_ref       |
+-----------+-------------------------+
|     1     |      1001,1002,1004     |
+-----------+-------------------------+
|     2     |      1005,1002,1001     |
+-----------+-------------------------
help:

+--------------------+
|help_id|  text      |
+--------------------+
| 1001 | sjfdisfidif |
+--------------------+
| 1002 |  dfdjdjdjjd |
+--------------------+
| 1003 | blafdsjdidjd|
+--------------------+
| 1004 | somethibngjd|
+--------------------+

我要完成的工作是从help_ref获取帮助ID

到目前为止,我做了什么:

SELECT *
FROM questions AS a
JOIN `help` AS b on find_in_set(b.`help_id`,a.`help_ref`) >0
WHERE b.`help_id` IN (1001,1002) // IM TRYING TO CHANGE THAT TO THE help_ref value
AND `question_id` = 1

那么如何将1001,1002的值更改为实际的help_ref字符串?

预期结果:

SELECT *
FROM questions AS a
JOIN `help` AS b on find_in_set(b.`help_id`,a.`help_ref`) >0
WHERE b.`help_id` IN (a.`help_ref`)
AND `question_id` = 1

help_id | text
1001    | sjfdisfidif
1002    | dfdjdjdjjd
1004    | somethibngjd

1 个答案:

答案 0 :(得分:1)

我想你想要这个:

SELECT h.*
FROM help h
INNER JOIN question q
    ON FIND_IN_SET(h.help_id, q.help_ref) > 0 AND q.question_id = 1;

Demo