Reputation: 299
I have seen some programmers using this condition 0='"="1'
and don't know why it returns true ?Can anyone explain it to me ? Thank you!
Upvotes: 2
Views: 110
Reputation: 425003
In order to compare an number with a string the string is cast to a number.
When casting a string to a number, mysql accepts all leading numbers and throws away the rest. When there are no leading numbers, the string is cast to zero:
0 = 'abc' -- true: string cast to 0
1 = '1abc' -- true: string cast to 1
Your string is evaluated as being 0 because there are no leading numbers.
Upvotes: 4
Reputation: 37365
The issue has no relation with "unusual" looks of the string. Yes. it's just primitive implicit type-conversion. You can do:
mysql> select 0='blablabla'; +---------------+ | 0='blablabla' | +---------------+ | 1 | +---------------+ 1 row in set, 1 warning (0.00 sec)
And see your reason:
mysql> show warnings; +---------+------+-----------------------------------------------+ | Level | Code | Message | +---------+------+-----------------------------------------------+ | Warning | 1292 | Truncated incorrect DOUBLE value: 'blablabla' | +---------+------+-----------------------------------------------+ 1 row in set (0.00 sec)
So same with your '"="1'
- it's just string and will be truncated during conversion to DOUBLE
causing zero-value.
Upvotes: 1