操作中遇到的问题,我要删除一条sql,delete from stu where id-1; 原本应该删除id=1的数据,结果等号写成了-号,最后是除了id为1的数据,剩下的全部删除了。这个减号是什么意义
-等同于<>,<>等同于!=
delete from stu where id-1 改为 delete from stu where id=1
MySQL的where是判断后面表达式结果的布尔值
。
0为false
,其他为true
。
你的id,都是无符号
的正整数(int
)。所以id-1这个表达式,只有id=1的时候,结果为0,是false
。 where 判断不成立,id=1的数据不删除。
然而,其他id的记录,id-1都一定大于0,都属于true
。都会被删除。
验证办法:
准备3条数据。
id, name
3, jack
4, tom
5, lisa
delete from t_name where id-3
补充:
准备3条数据。
id, name
3, jack
4, tom
5, lisa
执行delete from t_name where id-4
你会看到这个报错,BIGINT UNSIGNED value is out of range in xxxxxx
。 这是因为id=3的数据,3-4 = -1,-1这个数字,并不是无符号整型数字。
答题要对提问者负责,不要引导错了。
==回答完毕==