3

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

1
  • What is the error it is throwing?? Commented Apr 20, 2014 at 14:46

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

can we do it through looping and using TOP
What do you mean by looping? And as far as I know, MySQL doesn't support the TOP keyword.
Thanks .. I really don't knew it about TOP in #mysql ... Loop if can be done using SP

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.