源表:
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后面有小數
UPDATE 更新表 SET sj = current_timestamp(0);
已經有數據的表為什么沒法通過設置默認為current_timestamp(0)來添加時間。
還需要加update
series 序列即使有數據也可以添加進去自增的序列。