How to delete a row in mysql?

五迷三道 提交于 2019-12-25 14:10:06

问题


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    |
+-------+--------------+-----------+--------------------+---------------+

回答1:


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



回答2:


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_dates

DELETE FROM tbl WHERE 1 ORDER BY starting_date DESC LIMIT 2



回答3:


delete from table_name order by gp_no desc limit 2

Answer is clearly given in comments



来源:https://stackoverflow.com/questions/32755253/how-to-delete-a-row-in-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!