3

This is my users table:

http://ezinfotec.com/Capture.PNG

I need to select all rows those are not contain 2 in except column. How to write a query for this using php & Mysql.

The result i expect for this query is only return last row only.

Thank you.

Kevin
  • 41,694
  • 12
  • 53
  • 70
Parthasarathi
  • 99
  • 1
  • 6

3 Answers3

3

Don't store comma separated values in your table, it's very bad practice, nevertheless you can use FIND_IN_SET

SELECT
  *
FROM 
  users
WHERE 
  NOT FIND_IN_SET('2', except)
Ende Neu
  • 15,581
  • 5
  • 57
  • 68
  • 1
    can you please tell me why we should not store comma separate value in table? just want to know – Rakesh Shetty Sep 05 '14 at 06:34
  • 2
    Because you need an additional work to make them in readable or workable format. THat's why MySQL is relational database, and you can use the well-known relations e.g. many to many – Royal Bg Sep 05 '14 at 06:35
  • 1
    There's [this comprehensive SO post](http://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad/3653574#3653574) about storing comma separate values in a columns. – Ende Neu Sep 05 '14 at 06:36
1

Try this:

SELECT *
FROM users 
WHERE CONCAT(',', except, ',') NOT LIKE '%,2,%'
Saharsh Shah
  • 28,687
  • 8
  • 48
  • 83
0

this should work for you

SELECT *
FROM table
WHERE table.except NOT LIKE '%,2%'
OR table.except NOT LIKE '%2,%';
nvm-uli
  • 626
  • 1
  • 7
  • 14