在MariaDB中删除存在的SQL无效

pgccezyw  于 2022-11-11  发布在  其他
关注(0)|答案(1)|浏览(118)

我在MariaDB中运行这个select,它按预期工作,它只是一个带有exists的select:

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

现在,我尝试删除选中的行,因此我调整了语句:

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

但我得到一个错误:
SQL错误[1064] [42000]:(conn=6)您的SQL语法中有错误;查看与您的MariaDB服务器版本对应的手册,了解使用near 'd的正确语法
delete语句有什么问题?

xdnvmnnf

xdnvmnnf1#

在单表删除中,不能在表名后面使用别名。
您需要使用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

相关问题