Detailed explanation of mysql delete limit usage

1. Advantages of mysql delete limit

        The MySQL-only LIMIT row_count option for DELETE is used to tell the server the maximum number of rows to delete before a control command is returned to the client. This option is used to ensure that a DELETE statement does not take too much time. You can just repeat the DELETE statement until the number of related rows is less than the LIMIT value.

        If the DELETE statement includes an ORDER BY clause, the rows are deleted in the order specified in the clause. This clause only works in conjunction with LIMIT.

        For example, the following clause is used to find the row corresponding to the WHERE clause, use timestamp_column to sort, and delete the first (oldest) row:

DELETE FROM somelog WHERE user = 'jcole' ORDER BY timestamp_column LIMIT 1;

 

2. How to use delete limit

Single table syntax: delete [low_priority] [quick] [ignore] from tbl_name

[where where_definition]

[order by ...]

[limit row_count]

1. Delete all lines

        All rows can be deleted without dropping the table. This means that the structure, properties and indexes of the table are complete:

delete from table_name

        or:

delete * from table_name

2. Delete the specified amount of data, such as 0,30

delete from db limit 0,30

        Basically, I can't pass the test here. Delete from db limit 30 can pass the test. I don't know if it's a problem with my version.

It seems that the back of limit refers to how many records to delete, and it does not give the number of starting records like select!

Then if I want to limit 30, it should be deleted by default, which is similar to select * from db limit 0, 30,

select from `sheet1` where 1 limit 0, 1
delete from `sheet1` where 1 limit 1

        For a large number of records, we generally use limit 100

delete from tag_list where aid='6666' limit 100;

3. delete table connection does not support limit

        delete table connection does not support limit, how to deal with it

mysql> delete test1 from test1 join test2 on test1.id=test2.id limit 10;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'limit 10' at line 1
mysql>

        Solutions:

delete A FROM test1 A INNER JOIN (SELECT ID FROM test2 limit 10) B
on A.id=B.id ;

 

Article source: http://www.jb51.net/article/57416.htm

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326724971&siteId=291194637