Home > Enterprise >  Mysql 'Equal' (=) operator doesn't work when 'LIKE' operator does work
Mysql 'Equal' (=) operator doesn't work when 'LIKE' operator does work

Time:01-05

[MYSQL] Here are two queries that should bring out the same result. but,

SELECT * FROM my_table
WHERE id = 12345;

Result: Nothing

SELECT * FROM my_table
WHERE id LIKE 12345;

Result: Correct Answer

Even 'id' is not a string, but int. How come this possible?

CodePudding user response:

Because of MySQL's implicit casting rules, your query:

SELECT * FROM my_table WHERE id LIKE 12345;

is being converted to this:

SELECT * FROM my_table WHERE id LIKE '12345';

This is just the same as doing WHERE id = '12345', which of course is a valid comparison and will behave as you expect.

CodePudding user response:

The LIKE operator is a logical operator that tests whether a string contains a specified pattern or not. Then Use this :

SELECT * FROM my_table WHERE id LIKE '%' '12345' '%';
  •  Tags:  
  • Related