search

Home  >  Q&A  >  body text

MariaDB: Issues with SQL delete using exist clause

<p>I ran this select in MariaDB and it worked as expected, it was just a select with <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>Now, I'm trying to delete the selected rows, so I adapted the statement: </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>But I get an error: </p> <blockquote> <p>SQL Error [1064] [42000]: (conn=6) There is an error in your SQL Syntax; check the manual for your MariaDB server Version using correct syntax near 'd</p> </blockquote> <p><code>delete</code> What's wrong with the statement? </p>
P粉811329034P粉811329034471 days ago453

reply all(1)I'll reply

  • P粉752812853

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

    When deleting a single table, aliases cannot be used after the table name.

    You need to use JOIN instead of 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

    reply
    0
  • Cancelreply