-1

I have a column in my table called to_user that the content of it like this:

id     to_user

1.       1+2+3

2.         1

my query

select 
 * from notifications 
 where (type = 4 and to_user LIKE %+".$user_id."+%) 
 or (to_user REGEXP +?".$user_id."+? ) and status = 2

but it doesn't work .

sumit
  • 15,003
  • 12
  • 69
  • 110
S.M_Emamian
  • 17,005
  • 37
  • 135
  • 254
  • Does not make much sense. what is `+?` – David דודו Markovitz Jan 26 '17 at 08:51
  • Don't save CSV in a column http://stackoverflow.com/questions/41304945/best-type-of-indexing-when-there-is-like-clause/41305027#41305027 http://stackoverflow.com/questions/41215624/sql-table-with-list-entry-vs-sql-table-with-a-row-for-each-entry/41215681#41215681 – e4c5 Jan 26 '17 at 09:03

2 Answers2

1

you should enclose the strig for search in proper quotes (and remove + if is not part oth string of you sarch condition )

"select * from notifications where (type = 4 and to_user LIKE '%".$user_id."%') 
    or (to_user REGEXP '".$user_id."' ) and status = 2" ;

due the fact you are using string if you n need an exact macth is = istead of like and don't use regex too

ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
1
$sql = "SELECT * 
FROM notifications 
WHERE (type = 4 and to_user 
LIKE '%" . $user_id . "%') 
OR (to_user REGEXP '\+" . $user_id . "\+' ) 
AND status = 2";
Vitalij Mik
  • 542
  • 3
  • 6