更新内部加入冻结

时间:2013-04-03 12:15:22

标签: mysql sql

出于某种原因,此UPDATE查询会永久挂起。如果我替换为SELECT - 它会立即得到结果。

UPDATE table1 AS t1
INNER JOIN
(
    SELECT count(*) as total, left(number,7) as prefix, outcome FROM table1
    where outcome like '%Passed%'
    group by prefix 
    order by total desc limit 200
) AS t2 
ON t2.prefix = left(t1.number,7) AND t1.outcome = 'Fail'
SET t1.outcome = '', t1.status = 'NEW'

那里有什么问题?

3 个答案:

答案 0 :(得分:2)

尝试将ORDER BYLIMIt移至UPDATE的末尾。类似的东西:

UPDATE table1 AS t1
INNER JOIN
(
    SELECT count(*) as total, left(number,7) as prefix, outcome FROM table1
    where outcome like '%Passed%'
    group by prefix 
) AS t2 ON t2.prefix = left(t1.number, 7) AND t1.outcome = 'Fail'
SET t1.outcome = '', t1.status = 'NEW'
order by total desc 
limit 200;

请参阅UPDATE

的语法

答案 1 :(得分:1)

我会尝试这样的事情:

UPDATE table1
SET
  table1.outcome = '',
  table1.status = 'NEW'
WHERE
  outcome = 'Fail'
  AND 
  left(number,7) IN (SELECT * FROM (
    SELECT left(number,7) as prefix
    FROM table1
    WHERE outcome like '%Passed%'
    GROUP BY prefix 
    ORDER BY COUNT(*) desc limit 200
    ) s
  )

请参阅小提琴here

答案 2 :(得分:0)

您可以更新您要加入的列吗?也就是t1.outcome

将过滤器表达式t1.outcome = 'Fail'移出JOIN并进入WHERE子句。

UPDATE table1 AS t1
INNER JOIN
(
    SELECT count(*) as total, left(number,7) as prefix, outcome FROM table1
    where outcome like '%Passed%'
    group by prefix 
    order by total desc limit 200
) AS t2 
ON t2.prefix = left(t1.number,7)
SET t1.outcome = '', t1.status = 'NEW'
WHERE t1.outcome = 'Fail'