最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Mysql一些復(fù)雜的sql語句(查詢與刪除重復(fù)的行)

 更新時(shí)間:2017年05月20日 12:45:46   投稿:mdxy-dxy  
這篇文章主要介紹了Mysql一些復(fù)雜的sql語句(查詢與刪除重復(fù)的行),需要的朋友可以參考下

1.查找重復(fù)的行

SELECT * FROM blog_user_relation a WHERE (a.account_instance_id,a.follow_account_instance_id) 
IN (SELECT account_instance_id,follow_account_instance_id FROM blog_user_relation GROUP BY account_instance_id, follow_account_instance_id HAVING
 COUNT(*) > 1)

2.刪除重復(fù)的行(保留一條)

PS:因?yàn)閙ysql的delete,如果被刪的表的where條件里有in,且in里面也有此表,那就刪除不了。

/*創(chuàng)建個(gè)臨時(shí)表*/
CREATE TABLE blog_user_relation_temp AS
(
 SELECT * FROM blog_user_relation a WHERE 
 (a.account_instance_id,a.follow_account_instance_id) 
 IN ( SELECT account_instance_id,follow_account_instance_id FROM blog_user_relation GROUP BY account_instance_id, follow_account_instance_id HAVING COUNT(*) > 1)
 AND 
 relation_id 
 NOT IN (SELECT MIN(relation_id) FROM blog_user_relation GROUP BY account_instance_id, follow_account_instance_id HAVING COUNT(*)>1));

/*刪除數(shù)據(jù)*/
DELETE FROM `blog_user_relation` WHERE relation_id IN (SELECT relation_id FROM blog_user_relation_temp);

/*刪除臨時(shí)表*/
DROP TABLE blog_user_relation_temp;

相關(guān)文章

最新評論

新余市| 内乡县| 额济纳旗| 垫江县| 卫辉市| 淳安县| 龙泉市| 丽水市| 桃园县| 台东市| 合川市| 嵊泗县| 中西区| 岑溪市| 五常市| 克拉玛依市| 黄梅县| 澳门| 太仆寺旗| 抚顺市| 瓮安县| 上虞市| 象州县| 安吉县| 天等县| 搜索| 平阴县| 昌宁县| 孟津县| 乌拉特前旗| 宣威市| 广德县| 长顺县| 五台县| 锡林浩特市| 平湖市| 恩平市| 金堂县| 安塞县| 龙江县| 田林县|