0

I'm using below sql query for search values from db, but when I search 'study' it will returns the values 'caese-study', 'get-study-materials' as well. How can I use a query to search exact contains withing string column?

$names = 'study'; and the names column has values like comma separated, Ex: 'study, abc, new' so I need to search within that too

SELECT * FROM datatitle WHERE  names LIKE '%$names %' ;

SELECT * FROM datatitle WHERE  names regexp '(^|[[:space:]])$names([[:space:]]|$)';

I try with above two queries but didnt work as expect, pls advice?

1 Answers1

2

You should not be storing comma-separated values in a column. You should be using a junction/association table. You should fix the data model, if you can.

However, sometimes we cannot control other people's really bad decisions. MySQL has find_in_set():

SELECT dt.*
FROM datatitle dt
WHERE find_in_set(?, names) > 0;

Note that I have replaced the constant $names with a parameter. You should learn to use parameters to pass values into queries.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786