kxyz
kxyz

Reputation: 842

MySQL select equals condition work as like condition

I got today strange results, when I use query

 SELECT id, zip FROM `Locations` WHERE zip = '4000XXXXX'

Which simply fetchs id and zip code where zip code should be equals to condition. Schema:

zip int(11)

I have in table rows with zip values 4000 and expected result of this query is no result.

Simply - equals condition works as like condition where there is string and type of column is int. Whats going on ?

Picture

Upvotes: 1

Views: 516

Answers (1)

Ofir Winegarten
Ofir Winegarten

Reputation: 9365

What do you expect would be the result of a string and an int?

In order to make a sensable comparison, MySql tries to cast the string to an int by taking the left most digits.
In this case the casting gives 4000, hence you'll get all the rows with zip=4000

Upvotes: 3

Related Questions