如何从具有重复记录的MySQL表中删除单个值?
使用DELETE删除记录,但是如果您有多个重复的记录,则使用MySQLLIMIT限制删除,如以下语法所示:
语法
delete from yourTableName where yourColumnName=yourValue limit 1;
让我们首先创建一个表-
mysql> create table DemoTable -> ( -> Amount int -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values(50); mysql> insert into DemoTable values(100); mysql> insert into DemoTable values(50); mysql> insert into DemoTable values(70); mysql> insert into DemoTable values(90); mysql> insert into DemoTable values(50);
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+--------+ | Amount | +--------+ | 50 | | 100 | | 50 | | 70 | | 90 | | 50 | +--------+ 6 rows in set (0.00 sec)
这是从表中删除单个值的查询-
mysql> delete from DemoTable where Amount=50 limit 1;
让我们再次检查表记录-
mysql> select *from DemoTable;
这将产生以下输出-
+--------+ | Amount | +--------+ | 100 | | 50 | | 70 | | 90 | | 50 | +--------+ 5 rows in set (0.00 sec)