mysql去重多個欄位
阿新 • • 發佈:2020-10-23
源表:
select distinct id , name, phone from chongfubiao_quchong;
select distinct * from chongfubiao_quchong;
不合適的命令:
distinct(*)命令 #語法錯誤
[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*) from chongfubiao_quchong' at line 1
distinct(id)查出來的是一列
select distinct (id,name,phone) from chongfubiao_quchong;
[Err] 1241 - Operand should contain 1 column(s)
解決過程:
查詢多個欄位去重
參考連結:https://www.cnblogs.com/haizine/p/5803051.html
參考命令:select distinct ID,AA,BB from tName
有個疑問:
之前查詢無論
distinct (單個欄位)還是
distinct 多個欄位 好像查詢到的資料都是根據多個欄位去重的。不知道怎麼查詢顯示了?
將查到的去重資料抽到新表
INSERT into chongfubiao_quchong_copy (id, nam, phone)
select distinct * from chongfubiao_quchong ;
寫的錯誤語句
INSERT into chongfubiao_quchong_copy (id, nam, phone) values
select distinct * from chongfubiao_quchong ;
多了values 因為查到的是有id,name,phone還有資料
如果直接插要加values的
INSERT into chongfubiao_quchong_copy (id, nam, phone) values
(5, '3', '4')
總結:
去重語句
#每個欄位查詢
select distinct 列1名 , 列2名.. from 表名;
#所有查詢
select distinct * from 表名;
插入語句
#插入一條資料 INSERT into 插入表 (列1名 , 列2名..) value (資料1,資料2,..) ; #插入多條資料 INSERT into 插入表 (列1名 , 列2名..) values (資料1,資料2,..) ,(資料1,資料2,..); #插入 查詢到的資料 INSERT into 插入表 (列1名 , 列2名..) # 括號中東西不能用*替換 select distinct * from 查詢表 ;
在Navicat中複製一個表,並貼上一個。很快1m 600w資料(3列)(寫語句也一樣)
下面的是去重了之後插入的先查詢在插入
[SQL]INSERT into g....(id,xm,sfzh,jymc,jyjg,jgmc,jysj) SELECT distinct * from g....;
時間: 13.450s
受影響的行: 6796168
679w資料(函id、姓名、身份證號)的查詢和插入另一個表 用時13s。。。。
SELECT distinct * from g....;
查詢欄位出現大於1次的資料
select 欄位 from 表 group by 欄位 having count(欄位 ) >1;
更新時間 不要後面有小數的 。 不加0後面有小數