MySQL SELECT query for integers and integers +

My table name students

uid  |  name
-----+-------
10   |  John
11   |  Smith

      

** Data types: **

uid  int(11)
name varchar(256)

      

My request:

SELECT name FROM students WHERE uid = '10'

      

Returns :John

My second request:

SELECT name FROM students WHERE uid = '10someChar'

      

Returns :John

Why does the second query return John?

+3


source to share


2 answers


The uid column is an integer, and the value you pass in the where clause is coerced into an integer first ... and most integer conversion algorithms just grab the first set of digits they can find in the string (and ignore anything mismatch after it ) ... so it finds 10 and ignores the rest



+4


source


MySQL will automatically convert numbers to strings as needed, and vice versa.

It is also possible to convert a number to a string by explicitly using the CAST () function.

Type conversion

read.



MySQL ever tries to return something - even if it's the wrong type, it will automatically start.

You have to filter in PHP to check your business rule.

Postgresql must throw an exception

+1


source







All Articles