Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Table delete using order by and Limit in MySQL

Tags:

sql

mysql

When using delete statement for single table with Limit and ORDERBY its working fine

DELETE FROM test_users_table  ORDER BY test_users_table_cname DESC
LIMIT 5

Can we also achieve delete in multiple table using ORDER BY AND LIMIT option ..

Here is what I am using

  DELETE test_users_table, test_user_data_table FROM test_users_table 
    JOIN test_user_data_table 
    ON test_users_table.table1_id = test_user_data_table.table2_userid
    ORDER BY test_users_table.cname DESC
    LIMIT 5

For multiple table its throwing error

like image 934
Mr AJ Avatar asked Mar 28 '26 18:03

Mr AJ


1 Answers

According to the MySQL documentation, this is the syntax for deleting from multiple tables:

DELETE [LOW_PRIORITY] [QUICK] [IGNORE]
tbl_name[.*] [, tbl_name[.*]] ...
FROM table_references
[WHERE where_condition]

Additionally it states:

For the multiple-table syntax, DELETE deletes from each tbl_name the rows that satisfy the conditions. In this case, ORDER BY and LIMIT cannot be used.

Thus, you can delete from multiple tables. However, you cannot use the LIMIT keyword directly in it.

What's the assumed semantic for your query? Should the statement delete the first 5 entries from table A, table B or 5 entries from each table? This information might help to restructure your query.

like image 90
sebastian_oe Avatar answered Apr 01 '26 06:04

sebastian_oe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!