select * from users where username=' tester@tester.com' or '1'='1' and password='anything'
请问这条语句的where条件中,执行顺序是怎样?
1.username=' tester@tester.com' or '1'='1'
2.password='anything'
还有就是上面这条语句与
select * from users where username=' tester@tester.com' and password='anything' or '1'='1'
执行顺序和结果有什么不同呢?
and的优先级是要高于or的 所以这个语句的执行顺序是 先查 username 再查 '1'='1' and password='anything'
select * from users where username=' tester@tester.com' and password='anything' or '1'='1'
这里的话就是先查 username and password 再查1=1
就是说结果要么是一条数据 要么是整张表
假设两个条件都正确的数据只有一条
而最开始的sql 则可能出现查到一条(条件都正确)或者没数据的情况(两个条件都错误) 不会查到整表
SQL条件的执行是从右到左的
转自:http://blog.sina.com.cn/s/blog_4586764e0100mdif.html
关系型运算符优先级高到低为:NOT >AND >OR
所以,你可以理解成:
select *
from users
where username=' tester@tester.com' or ( '1'='1' and password='anything')
顺序:1、查 username=' tester@tester.com'
2、第1步不符合条件,再查: '1'='1'
3、第2步符合条件,再查:password='anything'
or(或:有一个满足条件即算满足) 和 and(且:两个都满足条件才算满足) 在sql中优先级是并列的,低于()
1. select * from users where username=' tester@tester.com' or '1'='1' and password='anything'
相当于 select * from users where (username=' tester@tester.com' or '1'='1' )and password='anything'
由于'1'='1'是永远成立的,等价于 select * from users where password='anything'
2.select * from users where username=' tester@tester.com' and password='anything' or '1'='1'
相当于:select * from users where (username=' tester@tester.com' and password='anything') or '1'='1'
由于'1'='1'是永远成立的,等价于 select * from users
sql的优先级看(),where,group by。。。。。。。。好像 or ,and是从左到右