很久没有去深究运算符的优先级了,今天写SQL解析思考了一下。
下面这条SQL真正的执行顺序是什么?
select * from taobao_category where type=1 or id=801 and type=3 and type=4
改成python脚本:
def test(type,id): print type==1 or id==801 and id==802 and id==803
测试结果是True
基本所有的语言的优先级都是 not>and>or,
所以上面那段python表达式实际是: (type==1) or (id==801 and id==802 and id==803),sql也就是:
select * from taobao_category where (type=1) or (id=801 and type=3 and type=4)
mysql> select * from taobao_category where type=0 and type=1 or id=324 and id =23 or id=830; +-----+----------+------+-----------+-----------------+------+------+--------+ | id | cid | type | parent_id | name | url | des | status | +-----+----------+------+-----------+-----------------+------+------+--------+ | 830 | 50024129 | 0 | 1402 | - | NULL | NULL | NULL | +-----+----------+------+-----------+-----------------+------+------+--------+ 1 row in set (0.00 sec)