我有一个调度表,我需要锁定然后读取。我目前正在尝试这样做:
LOCK TABLES db.schedule AS j_read READ;
SELECT * FROM db.schedule as j_read;
UNLOCK TABLES;
当我运行时,我收到错误:
错误代码:1100。表'j_read'未使用LOCK TABLES锁定
我已经查看了mysql表锁文档mysql docs,但我无法弄清楚为什么这对我不起作用。如果我没有别名,它就像这样
LOCK TABLES db.schedule READ;
SELECT * FROM db.schedule;
UNLOCK TABLES;
为什么这不起作用或如何正确地做到这一点的任何想法?
答案 0 :(得分:2)
我找到了做这项工作的方法。需要获取别名和表本身的锁
LOCK TABLES db.schedule READ,db.schedule AS j_read READ;
SELECT * FROM db.schedule as j_read;
UNLOCK TABLES;
答案 1 :(得分:0)
看看这一大块文档,你仍然需要从表中选择,但引用你的锁别名。你正在寻找最后一行。
mysql> LOCK TABLE t AS myalias READ;
mysql> SELECT * FROM t;
ERROR 1100: Table 't' was not locked with LOCK TABLES
mysql> SELECT * FROM t AS myalias;