今天同事寫了個刪除重複資料保留一條記錄的資料庫語句,問我錯在哪兒,正好給大家講講【注:以下語句只單對mysql資料庫】
語句 -- 問題:
delete from `show`
where id not in
( select max(id) from `show` where led = 43 and location = "<===" and status = 1
)and status = 1
sql語句(查詢)-- 正確:
select id from `show`
where id not in
(select * from (
select id from `show` group by led,location,status
) b
)and status = 1
sql語句(刪除)-- 正確:
delete from `show`
where id not in
(select * from (
select id from `show` group by led,location,status
) b
)and status = 1
1、為什麼要套這樣乙個select?因為 更新資料時使用了查詢,而查詢的資料又做更新的條件,mysql不支援這種方式
2、這句話中一定要取別名覺得辛苦就掃一掃:
SQL 刪除重複資料,只保留1條
if not object id tempdb.t is null drop table t gocreate table t id int,name nvarchar 1 memo nvarchar 2 insert t select 1,n a n a1 union all select 2,n...
mysql刪除重複資料
最近遇到刪除重複資料的問題,先分享一下解決辦法,如有不完善之處還望包涵!舉例如下 mysql select from table03 id name degree 1 fly 90 2 fly 90 3 fly 90 4 fly 80 5 wang 90 6 wang 90 7 wang 90 8 ...
MySQL 刪除重複資料
建表,插入資料,當然如果你不想測試一下,也可以直接用後面的刪除語句 create table if not exists tb01 id int unsigned primary key auto increment,name varchar 10 not null insert into tb01...