-2

every rows having multiple ids separated by comma. i want to select rows by id.

Database is like this:

R.No | User_Ids
-----|---------
1    | 38,323,45
-----|----------
2    | 382,535,338
-----|------------
3    | 32,38
-----|----------
4    |238,388,2394

if selected id is 38 then i need result as below:

R.No | User_Ids
-----|---------
1    | 38,323,45
-----|----------
3    | 32,38

i have used this query.

$sql="select User_Ids from table_name where User_Ids RLIKE '[[:<:]]".$Id."[[:>:]]'";

It is giving error as Illegal argument to a regular expression.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • 2
    now you know why placing link keys in a comma delimited list is madness – RiggsFolly Sep 18 '20 at 10:52
  • 2
    If its not too late, check out a database design for beginners course, then create a link table between this table and the user table – RiggsFolly Sep 18 '20 at 10:54
  • (The mentioned duplicate explains how this _can_ be done, but that changes rather little about the fact that this should probably rather be properly normalized, if at all still possible at this point.) – CBroe Sep 18 '20 at 11:01

1 Answers1

0

You can get desired result using FIND_IN_SET function:

SELECT * FROM test WHERE find_in_set(38, `Ids`);

Here you can test it

Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39