sql尋找相同資料與刪除相同記錄中的一行

來源:互聯網
上載者:User

1. 尋找資料表中某些欄位相同的資料,尋找相同資料:小技巧1:select * from tablename tn where((select count(*) from tablename where column1= tn.column1 and column2=tn.column2 ) > 1)提示:可以接order by 語句更方便查看

2.刪除相同記錄中的一行:小技巧1:當表中存在唯一索引值的情況:delete from tablename where id not in(select max(id) from tablename group by column1,column2)小技巧2:不存在唯一索引值的情況,最簡單的方法當然是新增一個列id,然後用1中的方法了。不過可以用中間表存取的方式:首選建一個和a表一樣結構的中間表,然後insert into b select distinct * from a,再刪除表a,delete from a,最後把b中的資料插入到a中, insert into a select * from b

例如:表caicai ,欄位id,title,content

 select * from caicai where title in ( select title from caicai group by title having count(*) >1) select * from caicai tn where((select count(*) from caicai where title= tn.title) > 1) delete from caicai where id not in(select max(id) from caicai group by title) SELECT infoid, title, brwsPath, indexed, indexed2 FROM infos WHERE (title IN(SELECT titleFROM infosGROUP BY titleHAVING COUNT(title) > 1)) ORDER BY title DESC

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.