0

I have to select a row from a table 'seller' which have two columns 'cat' and 'place' in which multiple ids are stored with commas

'seller' table
╔═══╦════════╦══════════════╗
║ id║cat     ║    place     ║
╠═══╬════════╬══════════════╣ 
║ 8 ║1,2     ║ 1,2,3,4      ║
╚═══╩════════╩══════════════╝

my mysql query

SELECT * FROM `seller` WHERE cat like '%1%' && place LIKE '%2%'

but i know it is bad query.if there is 12 it will treated as both 1 and 2. is there any good query??.

Siby Xavier
  • 136
  • 1
  • 1
  • 18

1 Answers1

2

Use FIND_IN_SET

SELECT * FROM `seller` 
WHERE find_in_set(1, cat) > 0
and find_in_set(2, place) > 0

But you should actually change your table design.

Never store multiple values in a single column!

That is a classic n to m relation. A better design would be

seller table
------------
id
name
...


categories table
----------------
id
name


seller_categories table
-----------------------
seller_id
category_id
Community
  • 1
  • 1
juergen d
  • 201,996
  • 37
  • 293
  • 362