warlock
V2EX  ›  数据库

SQL 语句的问题

  •  
  •   warlock · Apr 22, 2016 · 2249 views
    This topic created in 3853 days ago, the information mentioned may be changed or developed.

    select * from ttt where name = 'shuibianshu' * 'xxooxxoo'

    这条语句为什么可以把 ttt 表里的全部数据查出来? 后面星号两边的字符串可以随便乱输。

    15 replies  •  2016-08-04 10:54:45 +08:00
    EchoUtopia
    EchoUtopia
    Apr 22, 2016
    1
    我试的时候返回的星号后面那个条件的查询结果
    SilentDepth
    SilentDepth
    Apr 22, 2016
    2
    我是想说,那个字念 suí……
    lancerliu
    lancerliu
    Apr 22, 2016
    3
    数据库类型和版本呢?
    我执行这条语句是报错的
    warlock
    warlock
    OP
    Apr 22, 2016
    4
    @lancerliu Mysql 5.1.73
    warlock
    warlock
    OP
    Apr 22, 2016
    5
    @SilentDepth 哈哈 我的平翘舌一直有问题 ;)
    cdxy
    cdxy
    Apr 22, 2016
    6
    aaa' or 1=1 --

    注意--右边有空格,在 url 里面可以 --+

    完整语句:  select * from ttt where name='aaa' or 1=1 -- ' * 'xxx';
    qhxin
    qhxin
    Apr 22, 2016❤️1
    7
    'shuibianshu' * 'xxooxxoo'===>0

    试试: select 'shuibianshu' * 'xxooxxoo' from ttt where name = 'shuibianshu' * 'xxooxxoo'
    warlock
    warlock
    OP
    Apr 22, 2016
    8
    后面两个字符串中间的星号 是做了乘法运算 结果是 0 , 然后匹配字符串的时候,表里的字符串都被转成了 0 , 0=0 ,所以就都查出来了。 参考资料: http://stackoverflow.com/questions/22080382/mysql-why-comparing-a-string-to-0-gives-true
    ivvei
    ivvei
    Apr 22, 2016
    9
    Mysql 奇葩问题就是多啊
    liyj144
    liyj144
    Apr 22, 2016
    10
    因为'shuibianshu' * 'xxooxxoo' 的值是整数类型的 0 , 然后查询是 name 先做整型转换,然后和 0 对比,因为一般字符串(只要首字符不是数字的,比如 1qwe 这样的记录)都转成了 0 ,所以就造成了这种假象。
    lancerliu
    lancerliu
    Apr 22, 2016
    11
    @liyj144 @warlock 是对的,你这么试试: select *,'shuibianshu' * 'xxooxxoo' from ttt where name = 'shuibianshu' * 'xxooxxoo',然后就可以看到'shuibianshu' * 'xxooxxoo'的结果是 0 ,所以你的 sql 相当于 select * from ttt where name = 0
    loading
    loading
    Apr 22, 2016
    12
    这个是考试题?
    billlee
    billlee
    Apr 22, 2016
    13
    看了这个,我只想说,弱类型真可怕。
    SunnySuo
    SunnySuo
    Aug 4, 2016
    14
    楼主,我司招后端开发工程师( golang ), web 前端工程师、资深软件工程师,欢迎关注! http://v2ex.com/t/296530#reply3
    warlock
    warlock
    OP
    Aug 4, 2016
    15
    @SunnySuo 然而我在魔都。。。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4335 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 50ms · UTC 01:13 · PVG 09:13 · LAX 17:13 · JFK 20:13
    ♥ Do have faith in what you're doing.