这是我的语句:
delete from user_module where user_id in(select user_id from system_user where user_part=2)由于我的疏忽,我把system_user表里id字段写成user_id,但在mysql自动的命令行客户端里运行没有报错!执行结果:
mysql> delete from user_module where user_id in(select user_id from system_user where user_part=2);
Query OK, 0 rows affected (0.00 sec)我把语句该了一下,执行也没问题
mysql> select * from user_module where user_id in(select user_id from system_user where user_part=2);
Empty set (0.00 sec)把前面的查询去掉,就报错了
mysql> select user_id from system_user where user_part=2;
ERROR 1054 (42S22): Unknown column 'user_id' in 'field list'让人觉的奇怪,更让我觉得不可思议的是当我运行
delete from user_module where user_id in(select user_id from system_user where user_part=2)
mysql不但不报错,而且还把我的user_module 表的内容全都删除了!
不知道这个是不是mysql的bug!我的mysql版本:5.0.51b-community-nt
Ver 14.12 Distrib 5.0.51b, for Win32 (ia32)
delete from user_module where user_id in(select user_id from system_user where user_part=2)由于我的疏忽,我把system_user表里id字段写成user_id,但在mysql自动的命令行客户端里运行没有报错!执行结果:
mysql> delete from user_module where user_id in(select user_id from system_user where user_part=2);
Query OK, 0 rows affected (0.00 sec)我把语句该了一下,执行也没问题
mysql> select * from user_module where user_id in(select user_id from system_user where user_part=2);
Empty set (0.00 sec)把前面的查询去掉,就报错了
mysql> select user_id from system_user where user_part=2;
ERROR 1054 (42S22): Unknown column 'user_id' in 'field list'让人觉的奇怪,更让我觉得不可思议的是当我运行
delete from user_module where user_id in(select user_id from system_user where user_part=2)
mysql不但不报错,而且还把我的user_module 表的内容全都删除了!
不知道这个是不是mysql的bug!我的mysql版本:5.0.51b-community-nt
Ver 14.12 Distrib 5.0.51b, for Win32 (ia32)
提示没有这列了,被删除了??
ERROR 1054 (42S22): Unknown column 'user_id' in 'field list' 在字段集里没有user_id
MayBe you can change table name,or recreate new tables to test the bug as your said,then you will find out the result.
如果说"字段名=字段名变量"这个可以为“真”可以理解,但像楼主所说的这种情况的出现是不合理的,应该是个bug:select * from user_module where user_id in(select user_id from system_user where user_part=2);
/*若system_user where user_part=2里面没有符合条件的记录,则返回空的记录集;
若system_user where user_part=2里面只要有1条(包括多于1条)记录符合条件,则前面的主查询“select * from user_module where ”会返回所有记录(即后面查询条件全为“真”)
*/
所以,要解决,必须通过别名来引用:
select * from user_module a where a.user_id in(select b.user_id from system_user b where b.user_part=2);
这样就会报错了
否则,会出现前面所说的现象。
如果说"字段名=字段名变量"这个可以为“真”可以理解,但像楼主所说的这种情况的出现是不合理的,应该是个bug: select * from user_module where user_id in(select user_id from system_user where user_part=2);
/*若system_user where user_part=2里面没有符合条件的记录,则返回空的记录集;
若system_user where user_part=2里面只要有1条(包括多于1条)记录符合条件,则前面的主查询“select * from user_module where ”会返回所有记录(即后面查询条件全为“真”)
*/
所以,要解决,必须通过别名来引用:
select * from user_module a where a.user_id in(select b.user_id from system_user b where b.user_part=2); 这样就会报错了
否则,会出现前面所说的现象。
否则,进行delete时数据全删除了还不知道怎么回事呢
select * from user_module where user_id in(select user_id from system_user where user_part=2);
一般我们理解为
select * from user_module a where a.user_id in(select b.user_id from system_user b where user_part=2);
但由于表system_user没有user_id id字段所以数据解释为
select * from user_module a where a.user_id in(select a.user_id from system_user b where user_part=2);
从而造成以上的错误!
谢谢vinsonshen的帮助!