Mysql WHERE issue with comma separated list

I need help for this problem.

In the MYSQL table, I have a field:

Field  : artist_list  
Values : 1,5,3,401

I need to find all entries for artist uid 401

I'm doing it

SELECT uid FROM tbl WHERE artist_list IN ('401');

I have all the entries where the artist_list field values ​​are set to "401", but if I have 11,401, this query does not match.

Any idea?

(I cannot use the LIKE method for the user, because if the artist has 3 (corresponds to 30, 33, 3333) ...

+5
source share
3 answers

Short term solution

Use the FIND_IN_SET function :

SELECT uid 
  FROM tbl 
 WHERE FIND_IN_SET('401', artist_list) > 0

Long term solution

- , , " ", . , , :

ARTIST_LIST

  • artist_id ( , ARTIST)
  • uid ( , TBL)
+11
SELECT uid
FROM tbl
WHERE CONCAT(',', artist_list, ',') LIKE '%,401,%'

. .

+2

; . , , :

uid    artist
1      401
1       11
1        5
2        5
2        4
2        2

:

SELECT uid
  FROM table
 WHERE artist = 401

You should also study the normalization of the database, because what you have will simply cause more and more problems in the future.

+2
source

All Articles