Search code examples
mysqlsql-likeclause

Mysql LIKE clause find 1 and not 11


First I want to say, I'm sorry if this question have be asked already (haven't been able to find it then).

My problem is that I've got a LIKE clause in mysql where I have to find some numbers in a comma seperated row. (Yes I know comma separated rows aint a good thing to use, but I'm not the one who made the database structure).

So I have the following:

  SELECT pfd.*, pu.username, pu.user_email 
  FROM phpbb_profile_fields_data pfd
  LEFT JOIN phpbb_users pu ON pfd.user_id=pu.user_id 
  WHERE pfd.pf_pf_produktionsteknik LIKE '%" . ($id) . "%' 
      AND pu.user_type!=1 AND pfd.pf_status=2";

But the problem is here that it returns where the LIKE equal 1 (as it should) but also 11 (which it should not).

I have tried using field_in_set wich I couldn't get to work, and I'm now lost at what to do about it.


Solution

  • If your list is comma separated, can you look for number then comma, OR comma then number, or equalling number on its own. That should cover all cases including single number instances.