6

开始了:

mysql> LOCK TABLES radcheck WRITE;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT * FROM radcheck WHERE id NOT IN (
    ->     SELECT id FROM (
    ->         SELECT id FROM radcheck  WHERE attribute = 'Password' GROUP BY UserName HAVING COUNT(*) > 1
    ->      ) AS c
    -> );
ERROR 1100 (HY000): Table 'radcheck' was not locked with LOCK TABLES

怎么回事?

编辑

SET AUTOCOMMIT = 0
    -> ;
Query OK, 0 rows affected (0.00 sec)

mysql> LOCK TABLES radcheck WRITE;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT * FROM radcheck WHERE id NOT IN ( SELECT id FROM radcheck  WHERE attribute = 'Password' GROUP BY UserName HAVING COUNT(*) > 1 );
ERROR 1100 (HY000): Table 'radcheck' was not locked with LOCK TABLES



mysql> LOCK TABLES radcheck READ;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT * FROM radcheck WHERE id NOT IN ( SELECT id FROM radcheck  WHERE attribute = 'Password' GROUP BY UserName HAVING COUNT(*) > 1 );
ERROR 1100 (HY000): Table 'radcheck' was not locked with LOCK TABLES

pd:如果我不锁定表,查询工作正常。pd:这只是简化问题的一个例子……在现实生活中是一个删除……

4

3 回答 3

12

当您使用锁定表时,您需要锁定查询中的所有表。当您使用子查询时,它会创建一个表。而且您没有锁定它。因此,您会遇到错误。

参考: http ://dev.mysql.com/doc/refman/5.0/en/lock-tables.html

给内表一个别名

测试样品:

lock tables products as p1 write, products as p2 write ;
select product_id  from products as p1
where product_id  not in ( 
select product_id from products p2 where product_id in (1,2)
) 

可能你需要这样做:

lock tables radcheck as r1 write, radcheck as r2 write ;

 SELECT * FROM radcheck r1 WHERE id NOT IN (
SELECT id FROM (
  SELECT id FROM radcheck r2 WHERE attribute = 'Password' GROUP BY UserName HAVING COUNT(*) > 1) AS c
 );
于 2012-10-17T20:50:11.743 回答
1

可能您有 autocommit = 1 并在提交后释放表。

尝试:

SET AUTOCOMMIT = 0

在开始您的交易之前。

http://dev.mysql.com/doc/refman/5.0/es/innodb-and-autocommit.html

于 2012-10-17T20:42:00.773 回答
0

您正在为WRITE. 您需要在READ使用时锁定表,因为SELECT它只是从表中读取。

但是,您不应该真正锁定表,因为这会阻止并发。

编辑

您还需要使用别名,因为您在查询中两次使用同一个表。

IE

LOCK TABLES radcheck AS read1 READ, radcheck AS read2 READ;
SELECT * 
FROM radcheck AS read1
WHERE id NOT IN
   (
       SELECT id FROM radcheck AS read2
       WHERE attribute = 'Password'
       GROUP BY UserName
       HAVING COUNT(*) > 1
   );
于 2012-10-17T20:45:39.013 回答