Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL like search string starts with

Tags:

sql

Learning SQL. Have a simple table games with field title. I want to search based on title. If I have a game called Age of Empires III: Dynasties, and I use LIKE with parameter Age of Empires III: Dynasties, everything works fine, the search returns the record with that name. But if I search with Age of Empires III, it doesn't return any records:

SELECT * from games WHERE (lower(title) LIKE 'age of empires III'); 

This doesn't return anything. Should I be using something else instead of LIKE?

I am using MySQL.

like image 899
0xSina Avatar asked Feb 16 '13 07:02

0xSina


People also ask

What is %s in SQL query?

%s is a placeholder used in functions like sprintf. Check the manual for other possible placeholders. $sql = sprintf($sql, "Test"); This would replace %s with the string "Test".


2 Answers

SELECT * from games WHERE (lower(title) LIKE 'age of empires III'); 

The above query doesn't return any rows because you're looking for 'age of empires III' exact string which doesn't exists in any rows.

So in order to match with this string with different string which has 'age of empires' as substring you need to use '%your string goes here%'

More on mysql string comparision

You need to try this

SELECT * from games WHERE (lower(title) LIKE '%age of empires III%'); 

In Like '%age of empires III%' this will search for any matching substring in your rows, and it will show in results.

like image 103
Vishwanath Dalvi Avatar answered Sep 28 '22 09:09

Vishwanath Dalvi


You need to use the wildcard % :

SELECT * from games WHERE (lower(title) LIKE 'age of empires III%'); 
like image 43
misha Avatar answered Sep 28 '22 08:09

misha