Reputation: 132
Is it possible to delete one or more rows from a table in MySql ? I want to delete the last two rows from the following table.
+-------+--------------+-----------+--------------------+---------------+
| gp_no | no_of_member | amount | current_instalment | starting_date |
+-------+--------------+-----------+--------------------+---------------+
| 1 | 15 | 375000.00 | 2 | 2015-05-01 |
| 2 | 10 | 300000.00 | 1 | 2015-07-01 |
| 3 | 15 | 450000.00 | 5 | 2015-04-01 |
| 4 | 10 | 400000.00 | 0 | 2015-09-01 |
| 5 | 10 | 400000.00 | 0 | 2015-07-01 |
+-------+--------------+-----------+--------------------+---------------+
Upvotes: 0
Views: 194
Reputation: 3656
First you need to decide the order of the rows. Assuming your first column is unique;
DELETE FROM tbl WHERE 1 ORDER BY gp_no DESC LIMIT 2
But you might also want to expressly delete the last two rows according to date, say the most recent starting_date
s
DELETE FROM tbl WHERE 1 ORDER BY starting_date DESC LIMIT 2
Upvotes: 1
Reputation: 5
delete from table_name order by gp_no desc limit 2
Answer is clearly given in comments
Upvotes: 0
Reputation: 44874
If gp_no
is primary key and auto-incremented you can delete last 2 records as
delete from table_name order by gp_no desc limit 2
Upvotes: 1