当前位置: 首页 > news >正文

宁波网站建设网站新媒体运营培训学校

宁波网站建设网站,新媒体运营培训学校,全球最大的中文搜索引擎,做网络写手 哪个网站比较好需求说明:表中有 id, info, cnt 三个字段,对应的增量表多idu增量标记字段和时间戳字段ctimestamp。增量表中的 id 会有重复,其他字段 info、cnt 会不断更新,idu为增量标记字段,ctimestamp为IDU操作的时间戳。目的时要做…

需求说明:表中有 id, info, cnt 三个字段,对应的增量表多idu增量标记字段和时间戳字段ctimestamp。增量表中的 id 会有重复,其他字段 info、cnt 会不断更新,idu为增量标记字段,ctimestamp为IDU操作的时间戳。目的时要做到:
1)获取增量表中的时间戳字段(ctimestamp)为最新值对应的记录、进行id去重;
2)将第一步的查询结果进行脱IDU标记和时间戳字段、合并到最终的表中(不带IDU标记和时间戳字段)。

SQL查询根据时间戳字段和id字段获取最新值的记录

-- 表字段说明:id 会有重复,其他字段 info、cnt 会不断更新,idu为增量标记字段,ctimestamp为IDU操作的时间戳,需求是要获取时间戳字段(ctimestamp)为最新值对应的记录:

-- 脱IDU和时间戳以后的最终目的表(不带增量标记和时间戳字段)
drop table if exists test81;

-- 带IDU标记idu字段和时间戳字段ctimestamp,id字段可能存在重复的值的记录
drop table if exists test81_idu;

-- 基于 test81_idu 生成的 D 的记录,id唯一(去重后无重复记录)
drop table if exists test81_tmp2_d;

-- 基于 test81_idu 生成的 I、U 的记录,id唯一(去重后无重复记录)
drop table if exists test81_tmp3_iu;


-- 创建最终目的表、并构部分已有数据
create table test81 (
  id int not null, 
  info varchar(100), 
  cnt int,
  primary key(id));

 insert into test81 (id,info,cnt) values (1, 'aaa', 31);
 insert into test81 (id,info,cnt) values (2, 'bbb', 33);
 insert into test81 (id,info,cnt) values (3, 'ccc', 35);

注意:如果是GBase8a primary key(id)  主键约束无效。 

select * from test81;

查询结果:

+----+------+------+
| id | info | cnt  |
+----+------+------+
|  1 | aaa  |   31 |
|  2 | bbb  |   33 |
|  3 | ccc  |   35 |
+----+------+------+


-- 创建带IDU标记的表,id存在重复多条记录
create table test81_idu (
  id int not null, 
  info varchar(100), 
  cnt int,
  idu varchar(10),
  ctimestamp timestamp);
  
 insert into test81_idu (id,info,cnt,idu,ctimestamp) values (1, 'aaa', 31, 'I', '2023-10-27 12:31:31.123456789');
 insert into test81_idu (id,info,cnt,idu,ctimestamp) values (2, 'bbb', 33, 'I', '2023-10-27 12:33:33.123456789');
 insert into test81_idu (id,info,cnt,idu,ctimestamp) values (3, 'ccc', 35, 'I', '2023-10-27 12:35:35.123456789');
 insert into test81_idu (id,info,cnt,idu,ctimestamp) values (1, 'aaa', 50, 'U', '2023-10-27 12:50:50.123456789');
 insert into test81_idu (id,info,cnt,idu,ctimestamp) values (1, 'aaa', 41, 'U', '2023-10-27 12:41:41.123456789');
 insert into test81_idu (id,info,cnt,idu,ctimestamp) values (2, NULL, NULL, 'D', '2023-10-27 12:52:52.123456789');
 
 注意:MySQL支持上面9位(精确到纳秒的输入),只是数据库行为实际上丢弃后面3位,只取前面6位,但不会报错,能执行成功。但如果是GBase8a,则不行,只能指定6位:
 insert into test81_idu (id,info,cnt,idu,ctimestamp) values (1, 'aaa', 31, 'I', '2023-10-27 12:31:31.123456');
 insert into test81_idu (id,info,cnt,idu,ctimestamp) values (2, 'bbb', 33, 'I', '2023-10-27 12:33:33.123456');
 insert into test81_idu (id,info,cnt,idu,ctimestamp) values (3, 'ccc', 35, 'I', '2023-10-27 12:35:35.123456');
 insert into test81_idu (id,info,cnt,idu,ctimestamp) values (1, 'aaa', 50, 'U', '2023-10-27 12:50:50.123456');
 insert into test81_idu (id,info,cnt,idu,ctimestamp) values (1, 'aaa', 41, 'U', '2023-10-27 12:41:41.123456');
 insert into test81_idu (id,info,cnt,idu,ctimestamp) values (2, NULL, NULL, 'D', '2023-10-27 12:52:52.123456');


select * from test81_idu;

执行结果:
+----+------+------+------+---------------------+
| id | info | cnt  | idu  | ctimestamp          |
+----+------+------+------+---------------------+
|  1 | aaa  |   31 | I    | 2023-10-27 12:31:31 |
|  2 | bbb  |   33 | I    | 2023-10-27 12:33:33 |
|  3 | ccc  |   35 | I    | 2023-10-27 12:35:35 |
|  1 | aaa  |   50 | U    | 2023-10-27 12:50:50 |
|  1 | aaa  |   41 | U    | 2023-10-27 12:41:41 |
|  2 | NULL | NULL | D    | 2023-10-27 12:52:52 |
+----+------+------+------+---------------------+


表字段说明:id 会有重复,其他字段 info、cnt 会不断更新,idu为增量标记字段,ctimestamp为IDU操作的时间戳,需求是要获取时间戳字段(ctimestamp)为最新值对应的记录:


脱IDU标记合并处理(中间用多个到临时表)


-- 先查询一下根据时间戳字段和id进行处理,对id去重(同一个id的多条重复记录,只取时间戳最新的一条记录)
SELECT a.id,a.info,a.cnt,a.idu FROM test81_idu a,(
SELECT 
id, MAX(ctimestamp)AS time
FROM test81_idu
GROUP BY id
ORDER BY COUNT(*)DESC)b WHERE a.id=b.id and a.ctimestamp=b.time;

执行结果:
+----+------+------+------+
| id | info | cnt  | idu  |
+----+------+------+------+
|  3 | ccc  |   35 | I    |
|  1 | aaa  |   50 | U    |
|  2 | NULL | NULL | D    |
+----+------+------+------+

-- 如果用 INSERT INTO 则需要先创建目标表、如果用 SELECT INTO 则要保证目标表不存在(执行时会创建目标表)
DROP TABLE IF EXISTS test81_tmp2_d;
-- CREATE TABLE IF NOT EXISTS test81_tmp2_d  LIKE test81_idu;

-- 根据id和时间戳查询最新记录、并只显示idu='D'的结果
-- MySQL不支持的方式:
-- SELECT id,info,cnt INTO test81_tmp2_d FROM test81_idu;

-- MySQL能支持的方式:
DROP TABLE IF EXISTS test81_tmp2_d;
CREATE TABLE test81_tmp2_d 
(
SELECT a.id,a.info,a.cnt,a.idu FROM test81_idu a,(
SELECT 
id, MAX(ctimestamp)AS time
FROM test81_idu
GROUP BY id
ORDER BY COUNT(*)DESC)b WHERE a.id=b.id AND a.ctimestamp=b.time AND a.idu='D'
);

-- 查询结果:
select * from test81_tmp2_d;
+----+------+------+------+
| id | info | cnt  | idu  |
+----+------+------+------+
|  2 | NULL | NULL | D    |
+----+------+------+------+


-- 根据id和时间戳查询最新记录、并只显示idu='IU'的结果
-- MySQL能支持的方式
DROP TABLE IF EXISTS test81_tmp3_iu;
CREATE TABLE test81_tmp3_iu 
(
SELECT a.id,a.info,a.cnt,a.idu FROM test81_idu a,(
SELECT 
id, MAX(ctimestamp)AS time
FROM test81_idu
GROUP BY id
ORDER BY COUNT(*)DESC)b WHERE a.id=b.id AND a.ctimestamp=b.time AND (a.idu IN ('I','U'))
);

-- 查询结果:
select * from test81_tmp3_iu;
+----+------+------+------+
| id | info | cnt  | idu  |
+----+------+------+------+
|  3 | ccc  |   35 | I    |
|  1 | aaa  |   50 | U    |
+----+------+------+------+


-- 将'D'的数据从最终目标表删除
DELETE FROM test81 WHERE id in (SELECT DISTINCT id FROM test81_tmp2_d);


-- 'D' 记录删除后,查看目的表数据
select * from test81;
+----+------+------+
| id | info | cnt  |
+----+------+------+
|  1 | aaa  |   31 |
|  3 | ccc  |   35 |
+----+------+------+


-- 将'I'和'U'的数据插入或更新到最终的目标表
-- merge into, 目的表的关联id存在则update、不存在则insert:

-- GBase8a 使用 merge into (MySQL 不支持该语法)
-- merge into test81 t1 using test81_tmp3_iu tmp on t1.id=tmp.id when matched then update set t1.info=tmp.info,t1.cnt=tmp.cnt when not matched then insert(id,info,cnt)values(tmp.id,tmp.info,tmp.cnt);

-- MySQL 使用 insert into (GBase8a 不支持该语法)
INSERT INTO test81(id,info,cnt) SELECT id,info,cnt FROM test81_tmp3_iu ON DUPLICATE KEY UPDATE info=VALUES(info), cnt=VALUES(cnt);


-- 'I'和'U'合并到目的表后,查询结果
select * from test81;
+----+------+------+
| id | info | cnt  |
+----+------+------+
|  1 | aaa  |   50 |
|  3 | ccc  |   35 |
+----+------+------+


到此为止,整个脱IDU表级和时间戳的流程处理完毕。


文章转载自:
http://dinncochordamesoderm.ssfq.cn
http://dinncovirtuoso.ssfq.cn
http://dinncoretaliation.ssfq.cn
http://dinncoconjecture.ssfq.cn
http://dinncoparorexia.ssfq.cn
http://dinncoskidoo.ssfq.cn
http://dinncohaematocryal.ssfq.cn
http://dinncocrista.ssfq.cn
http://dinncocords.ssfq.cn
http://dinncocryptate.ssfq.cn
http://dinncoeverdurimg.ssfq.cn
http://dinncodamascus.ssfq.cn
http://dinncollc.ssfq.cn
http://dinncotelectroscope.ssfq.cn
http://dinncoeradicated.ssfq.cn
http://dinncoendocranium.ssfq.cn
http://dinncomonologue.ssfq.cn
http://dinncospecification.ssfq.cn
http://dinncodextrorotatory.ssfq.cn
http://dinncomoistureless.ssfq.cn
http://dinncoswarajist.ssfq.cn
http://dinnconofault.ssfq.cn
http://dinncoiridosmine.ssfq.cn
http://dinncodiffusor.ssfq.cn
http://dinncostayer.ssfq.cn
http://dinncoshaanxi.ssfq.cn
http://dinncounattainable.ssfq.cn
http://dinncopagoda.ssfq.cn
http://dinncounurged.ssfq.cn
http://dinncoarithmometer.ssfq.cn
http://dinncoangular.ssfq.cn
http://dinncospeaking.ssfq.cn
http://dinnconewsiness.ssfq.cn
http://dinncolaborer.ssfq.cn
http://dinncostorywriter.ssfq.cn
http://dinncotrattoria.ssfq.cn
http://dinncopid.ssfq.cn
http://dinncobrook.ssfq.cn
http://dinncoidentifier.ssfq.cn
http://dinncothar.ssfq.cn
http://dinncohippo.ssfq.cn
http://dinncohealth.ssfq.cn
http://dinncounbitter.ssfq.cn
http://dinncoexpose.ssfq.cn
http://dinncocliff.ssfq.cn
http://dinncoreign.ssfq.cn
http://dinncointertestamental.ssfq.cn
http://dinncosemifinal.ssfq.cn
http://dinncoyeo.ssfq.cn
http://dinncocaliduct.ssfq.cn
http://dinncoclingfish.ssfq.cn
http://dinncotoner.ssfq.cn
http://dinncohooknose.ssfq.cn
http://dinncomercaptide.ssfq.cn
http://dinnconightly.ssfq.cn
http://dinncopresswoman.ssfq.cn
http://dinncorarefy.ssfq.cn
http://dinncopotential.ssfq.cn
http://dinncomesopeak.ssfq.cn
http://dinncoxeranthemum.ssfq.cn
http://dinncodroop.ssfq.cn
http://dinncohungerly.ssfq.cn
http://dinncosciaenid.ssfq.cn
http://dinncodavis.ssfq.cn
http://dinnconaturphilosoph.ssfq.cn
http://dinncoklagenfurt.ssfq.cn
http://dinncognotobiotic.ssfq.cn
http://dinncopreexposure.ssfq.cn
http://dinncodesiccative.ssfq.cn
http://dinncomillidegree.ssfq.cn
http://dinncolola.ssfq.cn
http://dinncodownloading.ssfq.cn
http://dinncoacropathy.ssfq.cn
http://dinncotoothlet.ssfq.cn
http://dinncoscindapsus.ssfq.cn
http://dinncogizmo.ssfq.cn
http://dinncoprefabricate.ssfq.cn
http://dinncoorthographer.ssfq.cn
http://dinncodetractress.ssfq.cn
http://dinncomasterly.ssfq.cn
http://dinncotonsure.ssfq.cn
http://dinncoviatica.ssfq.cn
http://dinncophyllotactical.ssfq.cn
http://dinncobarbarise.ssfq.cn
http://dinncolcd.ssfq.cn
http://dinncopiggy.ssfq.cn
http://dinncoescheat.ssfq.cn
http://dinncoacceptably.ssfq.cn
http://dinncogoldsmith.ssfq.cn
http://dinncocaravaner.ssfq.cn
http://dinncogso.ssfq.cn
http://dinncohoropteric.ssfq.cn
http://dinncospake.ssfq.cn
http://dinncoarthropathy.ssfq.cn
http://dinncograf.ssfq.cn
http://dinncolineation.ssfq.cn
http://dinncomaluation.ssfq.cn
http://dinncotraumatism.ssfq.cn
http://dinncoshelterbelt.ssfq.cn
http://dinncofreight.ssfq.cn
http://www.dinnco.com/news/108412.html

相关文章:

  • 摄影网站设计素材产品软文范例100字
  • 上海定制建站网站建设上海网络seo优化公司
  • 博客网站需要的功能网站优化入门免费教程
  • php电子商务网站开发实例新品上市的营销方案
  • 创业网站推广怎么做qq群推广引流免费网站
  • 医疗网站建设公司百度快速收录办法
  • 徐州做网站费用南宁seo优化公司排名
  • 眉山网站建设兼职软文写作范文500字
  • 自适应网站怎么做下载优化大师
  • abcd设计官网百度优化大师
  • 医疗网站是否全部需要前置备案苏州seo网络推广
  • wordpress文件wordpress百度工具seo
  • 做网站空间哪个好2023第二波疫情已经到来
  • 怎么做网站可以注册的百度快速提交入口
  • 鄂州网站制作营销型网站的分类
  • 搭建小程序北京seo优化
  • 如何用div和css做购物网站seo网站快速排名外包
  • 做气球装饰可以上哪些网站seo及网络推广招聘
  • 专业代做网站国内最好用的免费建站平台
  • 图片做动画网站外贸独立站推广
  • 万维网网站正规代运营公司
  • 为什么现在建设银行要下载网站激活码企业建站免费模板
  • 白宫网站 wordpress百度推广联盟
  • 提供网站建设公司网络营销客服主要做什么
  • 软件开发网站建设百度竞价排名事件
  • 百度制作网站网络推广培训课程内容
  • 商丘市做网站的公司网站目录
  • 阿里巴巴网站推广怎么做免费软文发布平台有哪些
  • 苹果做安卓游戏下载网站好黄山seo
  • 代理加盟微信网站建设google play