sql去重多个字段(伪)、gp数据库的插入语句、更新时间(没有毫秒)

sql去重多个字段(伪)、gp数据库的插入语句、更新时间(没有毫秒)

源表:

image-20201023140000646

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 ;

image-20201023142626074

多了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....;

image-20201023154115311

 

查询字段出现大于1次的数据

select 字段 from 表 group by 字段 having count(字段 ) >1;

 

更新时间 不要后面有小数的 。 不加0后面有小数

UPDATE 更新表 SET sj = current_timestamp(0);

 

已经有数据的表为什么没法通过设置默认为current_timestamp(0)来添加时间。

image-20201104172700302

 

还需要加update

 

image-20201104172624545

series 序列即使有数据也可以添加进去自增的序列。

 

posted @ 2020-10-23 20:20  learnacode  阅读(886)  评论(0编辑  收藏  举报