mysql query showing wrong result

前端 未结 1 697
醉梦人生
醉梦人生 2021-01-24 23:07

I have a database table look like this

+======+===========+============+
|  ID  | user Name |user surname|
+======+===========+============+
| 100  |  name     |         


        
相关标签:
1条回答
  • 2021-01-25 00:04

    You are mixing types. ID is an integer (or number). You are comparing it to a string. So, MySQL needs to decide what type to use for the comparison. What types gets used? Well, a string? No. A number. The string is converted to a number, using the leading digits. So, it becomes 101 and matches.

    You should really only compare numbers to numbers, and strings to strings. You could try to write the code as:

    SELECT * FROM tableName WHERE ID = 101foo2
    

    However, you would get an error. Another possibility is to force the conversion to a string:

    SELECT * FROM tableName WHERE CAST(ID as CHAR) = '101foo2'
    
    0 讨论(0)
提交回复
热议问题