MariaDB:使用存在子句進行 SQL 刪除的問題
P粉811329034
P粉811329034 2023-08-30 23:16:01
0
1
398
<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粉811329034
P粉811329034

全部回覆(1)
P粉752812853

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

您需要使用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
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!