Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching for an integer value in a varchar field in MySQL

I have a table named user like below:

id || email
---------------------------------
1  || [email protected]
---------------------------------
2  || [email protected]
---------------------------------

Why is this:

[email protected]

result of this search?

SELECT * FROM user WHERE email = 1
like image 291
raminious Avatar asked Nov 16 '25 21:11

raminious


1 Answers

Because MySQL decides to convert the email to an integer. The rules are to convert leading characters to a number, until the characters are not valid numbers.

Here is a simple example:

select (case when '1abc' = 1 then 'a' else 'b' end)
like image 56
Gordon Linoff Avatar answered Nov 19 '25 10:11

Gordon Linoff