Search code examples
mysqlselectconcatenationwhere-clause

MySql - Select from a string concatenated with a comma


Suppose I have this MySql table:

id name city
1 John NYC
2 Albert London
3 Joanna Paris
4 Mike LA
5 Norton São Paulo
6 Pedro Rio de Janeiro
7 August Chicago
8 Carol Miami

So I get this string and I would like to filter people with these ids: "1,3,6,8" Sometimes the string I got is different: "1,5,4" or "3,6,5,8,1" etc. Any idea how to achieve this?


Solution

  • @Vanderlei based on your comment I will suggest not using a subquery, but a join because it performs much faster.

    select products_table.id,products from products_table 
    inner join table_people
    on products_table.id=table_people.id
    WHERE table_people.city = 'London';
    

    Demo: https://www.db-fiddle.com/f/7yUJcuMJPncBBnrExKbzYz/61