Lew Wei Hao
Lew Wei Hao

Reputation: 843

Wierd behaviour in My SQL query

Database

I have a database with columns shown above. Whenever, i try to execute the query

"SELECT * FROM `crosswordpuzzles` WHERE `Answer` LIKE '123' "

, mysql is able to return the respective row. However, when i execute the same query to find the other 2 rows,

"SELECT * FROM `crosswordpuzzles` WHERE `Answer` LIKE '456' "
"SELECT * FROM `crosswordpuzzles` WHERE `Answer` LIKE '789' "

My sql returns with 0 results. These rows have been inserted via a POST request from a previous html form.If However, I try to insert the same rows manually with phpmyAdmin, it is able to detect the new rows. What exactly is going on? I have been trying to figure out for a long time and can't understand why.

Upvotes: 0

Views: 40

Answers (2)

W Gunvant
W Gunvant

Reputation: 111

Try this one. This may help you.

SELECT * FROM crosswordpuzzles WHERE Answer LIKE '%789%'

The above SQL statement selects all records where Answer containing the pattern "789".

Upvotes: 1

Rahul Tripathi
Rahul Tripathi

Reputation: 172428

You dont have to use the LIKE keyword when you are looking for exact result. Instead try to use IN like

"SELECT * FROM `crosswordpuzzles` WHERE `Answer` IN ('789','123','456') "

and if you are searching individual search then simply search using = like

"SELECT * FROM `crosswordpuzzles` WHERE `Answer` = '789'"

Or it might be that there are some space so you can try like

"SELECT * FROM `crosswordpuzzles` WHERE TRIM(`Answer`) = '789'"
"SELECT * FROM `crosswordpuzzles` WHERE TRIM(`Answer`) = '123'"
"SELECT * FROM `crosswordpuzzles` WHERE TRIM(`Answer`) = '456'"

Upvotes: 0

Related Questions