SQL 语句的问题

2016-04-22 16:04:42 +08:00
 warlock

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

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

1159 次点击
所在节点    数据库
15 条回复
EchoUtopia
2016-04-22 16:14:07 +08:00
我试的时候返回的星号后面那个条件的查询结果
SilentDepth
2016-04-22 16:22:07 +08:00
我是想说,那个字念 suí……
lancerliu
2016-04-22 16:22:15 +08:00
数据库类型和版本呢?
我执行这条语句是报错的
warlock
2016-04-22 16:23:16 +08:00
@lancerliu Mysql 5.1.73
warlock
2016-04-22 16:24:05 +08:00
@SilentDepth 哈哈 我的平翘舌一直有问题 ;)
cdxy
2016-04-22 16:27:09 +08:00
aaa' or 1=1 --

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

完整语句:  select * from ttt where name='aaa' or 1=1 -- ' * 'xxx';
qhxin
2016-04-22 16:31:59 +08:00
'shuibianshu' * 'xxooxxoo'===>0

试试: select 'shuibianshu' * 'xxooxxoo' from ttt where name = 'shuibianshu' * 'xxooxxoo'
warlock
2016-04-22 16:34:35 +08:00
后面两个字符串中间的星号 是做了乘法运算 结果是 0 , 然后匹配字符串的时候,表里的字符串都被转成了 0 , 0=0 ,所以就都查出来了。 参考资料: http://stackoverflow.com/questions/22080382/mysql-why-comparing-a-string-to-0-gives-true
ivvei
2016-04-22 16:43:10 +08:00
Mysql 奇葩问题就是多啊
liyj144
2016-04-22 16:44:07 +08:00
因为'shuibianshu' * 'xxooxxoo' 的值是整数类型的 0 , 然后查询是 name 先做整型转换,然后和 0 对比,因为一般字符串(只要首字符不是数字的,比如 1qwe 这样的记录)都转成了 0 ,所以就造成了这种假象。
lancerliu
2016-04-22 20:17:40 +08:00
@liyj144 @warlock 是对的,你这么试试: select *,'shuibianshu' * 'xxooxxoo' from ttt where name = 'shuibianshu' * 'xxooxxoo',然后就可以看到'shuibianshu' * 'xxooxxoo'的结果是 0 ,所以你的 sql 相当于 select * from ttt where name = 0
loading
2016-04-22 20:24:48 +08:00
这个是考试题?
billlee
2016-04-22 20:39:11 +08:00
看了这个,我只想说,弱类型真可怕。
SunnySuo
2016-08-04 10:38:26 +08:00
楼主,我司招后端开发工程师( golang ), web 前端工程师、资深软件工程师,欢迎关注! http://v2ex.com/t/296530#reply3
warlock
2016-08-04 10:54:45 +08:00
@SunnySuo 然而我在魔都。。。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/273672

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX