首頁  >  問答  >  主體

MariaDB:使用存在子句進行 SQL 刪除的問題

<p>我在 MariaDB 中運行此選擇,它按預期工作,它只是一個帶有 <code>exists</code> 的選擇:</p> <pre class="brush:php;toolbar:false;">select * 從 pred_loan_defaults d where exists (select 1 from pred_loan_defaults d2 where d.exec_id = d2.exec_id and d.loan_identifier = d2.loan_identifier and d2.default_status = 1 and d.prediction_date > d2.prediction_date) order by loan_identifier, prediction_date</pre> <p>現在,我正在嘗試刪除所選的行,因此我調整了語句:</p> <pre class="brush:php;toolbar:false;">delete from pred_loan_defaults d where exists (select * from pred_loan_defaults d2 where d.exec_id = d2.exec_id and d.loan_identifier = d2.loan_identifier and d2.default_status = 1 and d.prediction_date > d2.prediction_date);</pre> <p>但我收到一個錯誤:</p> <blockquote> <p>SQL 錯誤 [1064] [42000]: (conn=6) 您的 SQL 中有錯誤 句法;檢查與您的 MariaDB 伺服器對應的手冊 在 'd 附近使用正確語法的版本</p> </blockquote> <p><code>delete</code> 語句有什麼問題? </p>
P粉811329034P粉811329034416 天前411

全部回覆(1)我來回復

  • P粉752812853

    P粉7528128532023-08-31 19:43:19

    單表刪除時表名後不能使用別名。

    您需要使用JOIN而不是WHERE EXISTS

    delete d
    FROM pred_loan_defaults AS d
    JOIN prod_loan_defaults AS d2
        ON d.exec_id = d2.exec_id 
            AND d.loan_identifier = d2.loan_identifier 
            AND d.prediction_date > d2.prediction_date
    WHERE d2.default_status = 1

    回覆
    0
  • 取消回覆