MySQL:使用同一个表从上一个查询的结果中删除

时间:2014-10-20 16:42:24

标签: mysql

我在尝试执行此查询时遇到错误,因为MySQL意识到我正在尝试通过一组来自同一个表上的JOIN的结果删除一些记录。

如何重写查询?

DELETE FROM hr_descr2 
WHERE
    id IN (SELECT 
        a.id
    FROM
        hr_descr2 a,
        hyperreview_descr b    
    WHERE
        a.titolo = b.titolo
        AND a.recensione != b.recensione
        AND a.recensione != ''
        AND b.recensione != ''
        AND ABS(LENGTH(a.recensione) - LENGTH(b.recensione)) > 40);

我也试图以这种方式重写查询,但它不起作用:

WITH temp AS(SELECT 
        a.id
    FROM
        hr_descr2 a,
        hyperreview_descr b    
    WHERE
        a.titolo = b.titolo
        AND a.recensione != b.recensione
        AND a.recensione != ''
        AND b.recensione != ''
        AND ABS(LENGTH(a.recensione) - LENGTH(b.recensione)) > 40)
DELETE FROM hr_descr2 
WHERE
    id IN (select id from temp);

2 个答案:

答案 0 :(得分:1)

首先,我不认为MySQL支持CTE,因此with在语法上不正确。

我没试过,但我认为这样的事情应该有用吗?

DELETE hr_descr2 
FROM hr_descr2 a,
    hyperreview_descr b    
WHERE
    a.titolo = b.titolo
    AND a.recensione != b.recensione
    AND a.recensione != ''
    AND b.recensione != ''
    AND ABS(LENGTH(a.recensione) - LENGTH(b.recensione)) > 40);

答案 1 :(得分:1)

而你可以使用

DELETE a FROM
    hr_descr2 a,
    hyperreview_descr b    
WHERE
    a.titolo = b.titolo
    AND a.recensione != b.recensione
    AND a.recensione != ''
    AND b.recensione != ''
    AND ABS(LENGTH(a.recensione) - LENGTH(b.recensione)) > 40