Mike
Mike

Reputation: 755

mysql: turn around like

i have a table like this

id, title
1, hello world
2, world hello

and i like to search for all ids that matches this php expression:

$searchstring = "hello world is yours"
if (strstr($searchstring,mysql(title)){echo found}

in mysql i often used this:

select id from table where title LIKE '%....%'

but i need it the other side:

select id from table where "hello word is yours" LIKE %title%

the result must be id 1 "hello word"

can anybody telle me the correct syntax?

Upvotes: 0

Views: 98

Answers (3)

juergen d
juergen d

Reputation: 204854

select id from table
where 'hello word is yours' LIKE concat('%', replace(title, ' ', '%'), '%')

Upvotes: 2

Mark Wilkins
Mark Wilkins

Reputation: 41252

As an alternative, you might consider using a Full Text Search. If you create an FTS index, it could be much more efficient and would provide for intuitive search strings:

SELECT id FROM tablename WHERE MATCH (title)
      AGAINST ('hello world is yours' IN BOOLEAN MODE);

Upvotes: 1

John Woo
John Woo

Reputation: 263813

use CONCAT

select id 
from tableName
where 'hello word is yours' LIKE CONCAT('%',title,'%')

Upvotes: 1

Related Questions