简体   繁体   English

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

[英]MySQL: delete from the result of a previous query using the same table

I get an error while trying to execute this query since MySQL realize that I'm trying to delete some records by a set of results which comes from a JOIN on the same table. 在尝试执行此查询时遇到错误,因为MySQL意识到我试图通过来自同一表上的JOIN的一组结果删除某些记录。

How can I rewrite the query? 如何重写查询?

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);

I've also tried to rewrite the query in this way but it doesn't work: 我也尝试过以这种方式重写查询,但是它不起作用:

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);

First, I don't think MySQL Supports CTEs, so the with is not syntactically correct. 首先,我认为MySQL不支持CTE,因此with在语法上不正确。

I haven't tried this but I think something like this should work? 我还没有尝试过,但是我认为类似的东西应该起作用?

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);

instead you can use 相反,您可以使用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM