i want to fetch comma separated record in mysql database

431 views Asked by At

In my database table I have 6 Columns id(auto),title,news,image,type I write this query.

select id,title,image 
from add_news 
where FIND_IN_SET('Travel','Music',type) 
ORDER BY id DESC

I am getting this error

Incorrect parameter count in the call to native function 'FIND_IN_SET'
2

There are 2 answers

0
Gordon Linoff On BEST ANSWER

I am guessing that type is a comma-delimited list. This is a very poor data format, and you should have a separate table with one row per type and article.

But, given the format, the correct syntax is:

select id, title, image
from add_news
where find_in_set('Travel', type) > 0 or
      find_in_set('Music', type) > 0
order by id desc;
0
Saharsh Shah On

Use IN operator instead of FIND_IN_SET()

Try this:

SELECT A.id, A.title, A.image 
FROM add_news A
WHERE A.type IN ('Travel', 'Music')
ORDER BY A.id DESC