简体   繁体   中英

fetching records against comma separated values from a table

I am using php in my web site. Here i want to fetch a record in such a way that match against a comma separated values. Please read below example
I have two tables.

  1. Table 1 say user, which have a column name say itemcode

    eg:

    id = 1 itemcode =3,10

  2. Table 2 say itemsList, which al so have itemcode field

    eg :

     id=1 itemcode= 1,3,7,5, id=2 itemcode= 4,9,10 id=3 itemcode= 1,3,10,11 id=4 itemcode 2,3,7,10 id=5 itemcode 1,2,8,9 

I need to display all the records from itemsList table which have itemcode 3 or 10

ie need to display the records with ids = 1, 2,3,4

You could use FIND_IN_SET function:

SELECT *
FROM yourtable
WHERE
  FIND_IN_SET(3, itemcode) OR
  FIND_IN_SET(10, itemcode)

Please see fiddle here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM