sql技巧(增册改查)
1 select * from wyl.t;
2 --将数据从t1导入t2
3 insert into t2(c1,c2) select c1,c2 from t1 where c1= xx and c2 = xx order by c1;
4 --使用to表的name来更新t1表的name
5 update t1 as a,t2 as b set a.name = b.name where a.tid = b.id;
6 --两表关联更新
7 update t_role_user as a,
8 (
9 select
10 id
11 from
12 t_user
13 where
14 departid in(
15 select
16 id
17 from
18 t_depart
19 where
20 length(org_code) = 9
21 )
22 ) as b
23 set a.roleid = '12345'
24 where
25 a.userid = b.id;
26 --自己和自己关联一更新
27 update t_depart as a,
28 (
29 select
30 id,
31 substring(org_code,1,6) org_code
32 from
33 t_depart
34 where
35 length(org_code)=8
36 and parent_depart_id is not null
37 ) as b
38 set a.parent_depart_id = b.id
39 where
40 substring(a.org_code,1,6) =
41 b.org_code
42 --两表关联删除,将删除两表中关联id并且t2表name为空的两表记录
43 delete a,b from t1 as a left join t2 as b on a.tid = b.id where b.name is null;
44 --奖统计结果插入到表
45 insert into se_stat_org (
46 record_date,
47 org_id,
48 org_name,
49 sign_cont_count,
50 sign_arri_cont_count,
51 sign_cont_money,
52 sign_arri_cont_money,
53 total_arri_cont_count,
54 total_arri_money,
55 publish_total_count,
56 project_count
57 ) select
58 *
59 from
60 (
61 select
62 '2012-06-09' record_date,
63 parent_org_id,
64 parent_org_name,
65 sum(sign_cont_count) sign_cont_count,
66 sum(sign_arri_cont_count) sign_arri_cont_count,
67 sum(sign_cont_money) sign_cont_money,
68 sum(sign_arri_cont_money) sign_arri_cont_money,
69 sum(total_arri_cont_count) total_arri_cont_count,
70 sum(total_arri_money) total_arri_money,
71 sum(publish_total_count) publish_total_count,
72 sum(project_count) project_count,
73 from se_stat_user
74 where date_format(record_date, '%y-%m-%d') = '2012-06-09'
75 group by parent_org_id
76 ) m
77
78 --三表关联更新
79 update se_stat_user a,
80 (
81 select
82 user_id,
83 sum(invest_org_count + financial_org_count + intermediary_org_count + enterprise_count) as common_count
84 from se_stat_user
85 where date_format(record_date, '%y-%m-%d') = '2012-06-09'
86 group by user_id
87 ) b,
88 (
89 select
90 user_id,
91 sum(establish_count + stock_count + merger_count + achieve_count) as project_count
92 from se_stat_user
93 where date_format(record_date, '%y-%m-%d') = '2012-06-09'
94 group by user_id
95 ) c
96 set a.common_count = b.common_count, a.project_count = c.project_count
97 where a.user_id = b.user_id
98 and a.user_id = c.user_id
99 and date_format(a.record_date, '%y-%m-%d') = '2012-06-09'
100 --带条件的关联更新
101 update se_stat_user a,
102 (
103 select
104 p.channel,
105 count(p.cont_id) as cont_count,
106 c.cust_mgr_id
107 from
108 (
109 select
110 channel,
111 cont_id
112 from sk_project
113 where project_status = 6
114 and date_format(audit_time, '%y-%m-%d') = '2012-06-11'
115 ) p
116 inner join se_contract c on p.cont_id = c.cont_id
117 group by p.channel, c.cust_mgr_id
118 ) b
119 set
120 a.stock_count = case when b.channel = 2 then b.cont_count else 0 end,
121 a.establish_count = case when b.channel = 3 then b.cont_count else 0 end,
122 a.achieve_count = case when b.channel = 4 then b.cont_count else 0 end,
123 a.brand_count = case when b.channel = 5 then b.cont_count else 0 end,
124 a.merger_count = case when b.channel = 6 then b.cont_count else 0 end
125 where
126 a.user_id = b.cust_mgr_id
127 and date_format(a.record_date, '%y-%m-%d') = '2012-06-11'
128 --加索引
129 alter table project add index index_user_id (user_id),
130 add index index_project_status (project_status);
131 --删除列
132 alter table project drop column project_status,
133 drop column expect_return,drop column currency;
134 --增加列
135 alter table project
136 add column dict_id int default null comment 'xxx' after project_site,
137 add column introduce text default null comment 'xx' after dict_id,
138 add column stage int default null comment 'xx' after id,
139 add column attach_uri varchar(8) default null comment 'xxx' after introduce;
140 --修改列,一般用modify修改数据类型,change修改列名
141 alter table project change dict_id dict_id1 int not null,
142 modify project_status tinyint not null comment 'xxx';
143 --1.总体累计统计 对员工的工资,人数进行总体累计统计
144 select
145 employee_id,
146 sum(salary) over(order by employee_id) sal,
147 count(*) over (order by employee_id) num,
148 sum(salary) over() total_sal,
149 count(*) over() total_num,
150 from hr.employees;
151 --2.分组累计统计 对各个部门中的员工工资,人数进行分组累计统计
152 select
153 department_id,
154 employee_id,
155 sum(salary) over(partition by department_id order by employee_id) sal,
156 count(*) over(partition by department_id order by employee_id) sum
157 from hr.employees;
sql技巧(增册改查)的更多相关文章
- WinForm 对Web Api 增 册 改 查 的基本操作
WebApi代码: public class ValuesController : ApiController { Entities db=new Entities(); // GET api/val ...
- 好用的SQL TVP~~独家赠送[增-删-改-查]的例子
以前总是追求新东西,发现基础才是最重要的,今年主要的目标是精通SQL查询和SQL性能优化. 本系列主要是针对T-SQL的总结. [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础] ...
- iOS sqlite3 的基本使用(增 删 改 查)
iOS sqlite3 的基本使用(增 删 改 查) 这篇博客不会讲述太多sql语言,目的重在实现sqlite3的一些基本操作. 例:增 删 改 查 如果想了解更多的sql语言可以利用强大的互联网. ...
- iOS FMDB的使用(增,删,改,查,sqlite存取图片)
iOS FMDB的使用(增,删,改,查,sqlite存取图片) 在上一篇博客我对sqlite的基本使用进行了详细介绍... 但是在实际开发中原生使用的频率是很少的... 这篇博客我将会较全面的介绍FM ...
- django ajax增 删 改 查
具于django ajax实现增 删 改 查功能 代码示例: 代码: urls.py from django.conf.urls import url from django.contrib impo ...
- ADO.NET 增 删 改 查
ADO.NET:(数据访问技术)就是将C#和MSSQL连接起来的一个纽带 可以通过ADO.NET将内存中的临时数据写入到数据库中 也可以将数据库中的数据提取到内存中供程序调用 ADO.NET所有数据访 ...
- MVC EF 增 删 改 查
using System;using System.Collections.Generic;using System.Linq;using System.Web;//using System.Data ...
- 网站的增 / 删 / 改 / 查 时常用的 sql 语句
最近在学习数据库 php + mysql 的基本的 crud 的操作,记录碰到的坑供自己参考.crud中需要用到的sql语句还是比较多的,共包括以下几个内容: 查询所有数据 查询表中某个字段 查询并根 ...
- JDBC中执行sql语句的 增 , 删 , 改 , 查 的方法
executeQuery() : 执行 SELECT 语句,它几乎是使用最多的 SQL 语句 executeUpdate() : 执行 INSERT.UPDATE 或 DELETE 语句以及 S ...
随机推荐
- BugKu之备份是个好习惯
题目:备份是个好习惯 思路分析 打开题目,看到一个字符串. 联系到题目,就猜到肯定是源代码泄露,用工具扫一下,发现了index.php.bak,验证了我的猜想,下载下来看看. <?php /** ...
- prometheus(3)之grafan可视化展现
可视化UI界面Grafana的安装和配置 Grafana介绍 Grafana是一个跨平台的开源的度量分析和可视化工具,可以将采集的数据可视化的展示,并及时通知给告警接收方.它主要有以下六大特点: 1. ...
- IDEA中Update resources和Update classes and resources、Redeploy、Restart server的区别
选项 描述 update resources 所有更改的资源都会更新(HTML,JSP,JavaScript,CSS和图像文件) update classes and resources 更改的资源将 ...
- v-bind使用
v-bind基本使用 动态地绑定一个或多个属性,或者绑定一个组件 prop 到表达式. 语法:v-bind:属性名 = 属性值 <!-- 绑定一个 attribute --> <im ...
- Mac下查看 Java 安装目录位置和安装数量
/usr/libexec/java_home -V 第一个红框是安装数量, 第二个红框是目前正在使用的 JDK 版本位置
- 【ASP.NET Core】体验一下 Mini Web API
在上一篇水文中,老周给大伙伴们简单演示了通过 Socket 编程的方式控制 MPD (在树莓派上).按照计划,老周还想给大伙伴们演示一下使用 Web API 来封装对 MPD 控制.思路很 Easy, ...
- R数据分析:潜类别轨迹模型LCTM的做法,实例解析
最近看了好多潜类别轨迹latent class trajectory models的文章,发现这个方法和我之前常用的横断面数据的潜类别和潜剖面分析完全不是一个东西,做纵向轨迹的正宗流派还是这个方法,当 ...
- [bzoj2743]采花
预处理出每一个点下一个相同颜色的位置,记为next,然后将询问按左端点排序后不断右移左指针,设要删除i位置,就令f[next[next[i]]+1,同时还要删除原来的标记,即令f[next[i]]-1 ...
- [loj2977]巧克力
先考虑第一个问题,即求最小的巧克力块数 将这张网格图建图(仅对$c_{i,j}\ne -1$的位置建点),即求点数最少的连通块(的点数)使得存在$k$个不同的$c_{i,j}$ (以下$c$仅用一维数 ...
- [atAGC023F]01 on Tree
对每一个节点维护一个序列,初始即自己(长度为1),并记$a_{i}$和$b_{i}$分别为第$i$个点序列上0和1的个数(也需要存储具体的序列) 考虑$\frac{b_{i}}{a_{i}}$最小中最 ...