我试图执行以下sql,由于多个进程之间的同步,我首先锁定了表
lock tables Report write;
SELECT id,taskTime,timeout,maxExecutionTimes,currentOwner, tenantID,
case when tenantID = 'NoValue' then 1 else 0 end tenantOrder,
case when tenantID in (select distinct tenantID from Report where state = 2) then 1 else 0 end mspInHandling
FROM Report WHERE state=2 and maxExecutionTimes>executionTimes
order by tenantOrder desc, mspInHandling limit 1
虽然表已被锁定,但是此sql语句始终给我一个错误,例如:“表报表未被锁定表锁定”
如果我不锁定表,则SQL语句可以很好地运行,我认为这可能与嵌套(select distinct tenantID from Report where state = 2
)有关,但找不到出路。
有人可以帮忙吗?预先感谢。
答案 0 :(得分:0)
使用子查询时,需要给子查询表加上别名,并使用相同的别名锁定该表。您可以尝试一下吗?:
lock tables Report write, Report as r2 read;
SELECT id,taskTime,timeout,maxExecutionTimes,currentOwner, tenantID,
case when tenantID = 'NoValue' then 1 else 0 end tenantOrder,
case when tenantID in (select distinct tenantID from Report as r2 where state = 2) then 1 else 0 end mspInHandling
FROM Report WHERE state=2 and maxExecutionTimes>executionTimes
order by tenantOrder desc, mspInHandling limit 1
在上述sql中,我锁定表Report as r2 read
并为子查询Report as r2
加上别名