网站首页 > 教程文章 正文
针对一个上线的项目进行数据库优化,以便后期统计,遇到一个数据填充的问题,在此记录一下,各位如果也有这种问题,欢迎一起交流。
表结构:
当我从其它数据源使用sql来填充这个表数据时,from_id都是null,因此要使用update来对from_id进行补充。
update t_ch_test t set t.from_id =
(select max(a.id) from t_ch_test a where a.node_id = t.node_id and a.id < t.id)
使用update select来自连接进行更新操作。这个sql在oracle中执行是没有任何问题的,然后,坑爹的是,这个sql在postgresql中编译都报错。而我们项目组用的最多的就是postgresql,因此无语了。
后来百度一下,发现postgresql在update时不支持表别名alis,所以最开始想的是把别名去掉,如下:
update t_ch_test set from_id =
(select max(a.id) from t_ch_test a where a.node_id = node_id and a.id < id)
sql执行是没有问题了,但一查结果,发现from_id全部为null,怎么回事儿呢。
后来在同事的指导下,a.id < id换成了a.id < 100,执行成功,from_id也有值了,但这个100不能是一个固定值呀。后来自己各种试,终于试出来了,不用表别名,直接用表名代替,如下:
update t_ch_test set from_id =
(select max(a.id) from t_ch_test a where a.node_id = t_ch_test.node_id and a.id < t_ch_test.id)
终于成功了。
https://www.linuxprobe.com/sql-update-select.html
猜你喜欢
- 2024-12-28 《黑屏死机》 电脑黑屏死机
- 2024-12-28 SQL轻松入门(1):增删改与简单查询
- 2024-12-28 【数据库】Upsert = Update or Insert
- 2024-12-28 SQL查询语句大全(一) sql查询语句命令大全
- 2024-12-28 大数据开发基础之SQL语句基本操作
- 2024-12-28 SQL CASE WHEN的用法 sql case when 的用法详解
- 2024-12-28 高并发下如何防重? 如何防止高并发
- 2024-12-28 最全汇总|SQL Server与Access数据库sql语法差异
- 2024-12-28 SQL left join 左表合并去重技巧总结
- 2024-12-28 为什么一条UPDATE语句有索引反而更慢
- 最近发表
- 标签列表
-
- location.href (44)
- document.ready (36)
- git checkout -b (34)
- 跃点数 (35)
- 阿里云镜像地址 (33)
- qt qmessagebox (36)
- md5 sha1 (32)
- mybatis plus page (35)
- semaphore 使用详解 (32)
- update from 语句 (32)
- vue @scroll (38)
- 堆栈区别 (33)
- 在线子域名爆破 (32)
- 什么是容器 (33)
- sha1 md5 (33)
- navicat导出数据 (34)
- 阿里云acp考试 (33)
- 阿里云 nacos (34)
- redhat官网下载镜像 (36)
- srs服务器 (33)
- pico开发者 (33)
- https的端口号 (34)
- vscode更改主题 (35)
- 阿里云资源池 (34)
- os.path.join (33)