MySQL-SELECT…WHERE ID IN(SELECT ...)

时间:2019-12-11 05:23:05

标签: mysql

我有以下查询

SELECT * FROM table WHERE id IN (5,4,3,1,6)

我想再次从SELECT中获得该值“ 5,4,3,1,6”。

SELECT * FROM table WHERE id IN (SELECT popuplarList FROM Settinng WHERE id =1 )

但是我只得到5号帖子。我想从“ 5,4,3,1,6”中获取所有帖子。

是否可以通过其他逻辑或其他方式解决此问题?

非常感谢

2 个答案:

答案 0 :(得分:3)

尝试FIND_IN_SET方法

SELECT * FROM table WHERE FIND_IN_SET(id, (SELECT popuplarList FROM Settinng WHERE id =1 ))

答案 1 :(得分:1)

请尝试以下操作:

  • 第一个解决方案:
    select * 
    from table 
    where (
        select CONCAT(",", popuplarList, ",")
        from Settinng where id = 1
    ) like concat("%,", id, ",%")
  • 第二种解决方案:
    select * from table WHERE FIND_IN_SET(`id`, (
       select CONCAT(",", popuplarList, ",") from Settinng where id = 1)
    )