Skip to content
Advertisement

Right syntax to use near ORDER BY when I try to delete some number of rows

When I try to delete some rows from tables (rows got from joined tables) (Ex : if I get 10 records, then need to delete first 2 records) using ORDER BY ASC and LIMIT = 2.

But getting error

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ‘ORDER BY sales_flat_quote.entity_id ASC LIMIT 2’ at line 9

Below is my code:

DELETE table1,table2,table3
FROM table1
LEFT JOIN table2 on table1.entity_id=table2.quote_id
LEFT JOIN table3 on table1.entity_id=table3.quote_id
WHERE table1.entity_id <= 101
ORDER BY table1.entity_id ASC LIMIT 2;

Advertisement

Answer

Solution :

DELETE table1,table2,table3
FROM table1
INNER JOIN (
            SELECT entity_id 
            FROM table1
            WHERE entity_id<= 101
            ORDER BY entity_id ASC
            LIMIT 2
           ) sub1
ON table1.entity_id=sub1.entity_id
LEFT JOIN table2 ON table2.quote_id = table1.entity_id
LEFT JOIN table3 ON table1.entity_id = table3.quote_id
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement