MariaDB:使用存在子句进行 SQL 删除的问题
P粉811329034
P粉811329034 2023-08-30 23:16:01
0
1
391
<p>我在 MariaDB 中运行此选择,它按预期工作,它只是一个带有 <code>exists</code> 的选择:</p> <pre class="brush:php;toolbar:false;">select * from 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学习者快速成长!