V
V
Vit2017-10-11 21:51:59
MySQL
Vit, 2017-10-11 21:51:59

How to remove records from a table?

You need to leave 100 records in the table, deleting everything else.
Sample SQL query:

delete from table where shop_id = '12' limit 100, 7000

Mistake:
Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 check the manual that corresponds to your MySQL server version for the right syntax to use near ' 7000'

Tell me how to do it right. Thanks for answers.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
vs02, 2017-10-11
@reech

DELETE does not allow using offset in the request, one solution is to use a nested request

DELETE FROM table WHERE id IN(SELECT id FROM (SELECT * FROM table WHERE shop_id=12 LIMIT 100, 7000)  table)

V
Vyacheslav Uspensky, 2017-10-11
@Kwisatz

limit remove

D
Dmitry Bay, 2017-10-11
@kawabanga

Here is another simple solution:
1) query for the number of records, "select count(id) from table where shop_id = '12' "
2) count how many records to delete - count_delete = count - 100
3) make a query "delete from table where shop_id = '12' limit :count_delete"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question