Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

mysql select statement IN

I am having two table

Table 1 having a field

| id | book_ids |
| 1  |  1,2,3   |

Table 2 have all the book Ids

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

select * 
from table 2 
where book_id in (select book_ids from table 1 where id=1) ;

this statement not returning all the book ids from table 2 having id 1,2,3

Can anyone help

>Solution :

You could use the FIND_IN_SET() function:

select * 
from table 2 
where FIND_IN_SET(book_id, (select book_ids from table 1 where id=1)) > 0;

Read the documentation I linked to for details on how that function works.

But only do this if your table remains small. Using this function spoils any opportunity to optimize the query with an index, so the larger your table gets, the performance will grow worse and worse.

Also FIND_IN_SET() doesn’t work the way you expect if there are spaces in your comma-separated list.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading