前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >挑战进阶级SQL,你能理解几个

挑战进阶级SQL,你能理解几个

作者头像
挨踢小子部落阁
发布2019-08-20 16:32:34
4960
发布2019-08-20 16:32:34
举报
1、复制数据库表数据操作
代码语言:javascript
复制
//微信公众号:关注挨踢小子 

1、如果是整个表复制表达如下:

insert into table1 select  * from table2

2、如果是有选择性的复制数据表达如下:

insert into table1(column1,column2,column3...) 
select column1,column2,colunm3...from table2

3、一个数据库中的表中的数据复制到另一个数据库中的一个表,使用方法如下:

insert into 数据库A.dbo.table1(col1,col2,col3...) 
select col1,col2,col3... from 数据库B.dbo.table2 

4、直接复制现有表重命名

CREATE  TABLE 表名  AS SELECT 语句;
2、sql的子查询可以放到查询字段之中
代码语言:javascript
复制
//微信公众号:关注挨踢小子 
select  name,age,(select date from tableB)
from tableA  where age="12";
3、一张表中两个字段值互换(重点思想)
代码语言:javascript
复制
//微信公众号:关注挨踢小子 
update table a, table b 
set a.filed1= b.field2, a.field2= b.field1
where a.id = b.id
4、实现数据库两列数据的上下移动操作
代码语言:javascript
复制
//微信公众号:关注挨踢小子 
update test set priority=   

(case when id=1  then    

(select priority from test where id=2)    

when id=2 then    

(select priority from test where id=1) end)   

 where id=1 or id=2; 
5、mysql合并字符串
代码语言:javascript
复制
//微信公众号:关注挨踢小子 
concat(string1,string2,string3,…)

select concat( '1', '01','02')

// 10102  

concat_ws(separator,str1,str2,...)

select concat_ws('-','1','2','3')

// 1-2-3
group_concat()
group_concat( 
[distinct] 要连接的字段 
[order by 排序字段 asc/desc ] 
[separator '分隔符'] 
)

// 例如:
SELECT  group_concat(content  ORDER BY id DESC separator '-') from t_noteinfo 
6、删除重复数据,只保留最小ID编号
代码语言:javascript
复制
delete tablename where id not in(select min(id) 
from tablename group by name,………………)
7、两张关联表,删除主表中已经在副表中没有的信息
代码语言:javascript
复制
delete from info where not exists 

(select * from infobz where info.infid=infobz.infid)
8、日程安排提前五分钟提醒(日期操作)
代码语言:javascript
复制
select * from 日程安排 

where datediff('minute',f 开始时间,getdate())>5
9、查询出每门课都大于90 分的学生姓名
代码语言:javascript
复制
select name from table group by name having min(fenshu)>90
10、数据结构转换形式
代码语言:javascript
复制
//微信公众号:关注挨踢小子
原表数据结构
 year   month amount
 1991   1     1.1
 1991   2     1.2
 1991   3     1.3
 1991   4     1.4
 1992   1     2.1
 1992   2     2.2
 1992   3     2.3
 1992   4     2.4

查成这样一个结果

 year m1   m2   m3   m4
 1991 1.1 1.2 1.3 1.4
 1992 2.1 2.2 2.3 2.4

select year, 
    (select amount from   aaa m where month=1   and m.year=aaa.year) as m1,
    (select amount from   aaa m where month=2   and m.year=aaa.year) as m2,
    (select amount from   aaa m where month=3   and m.year=aaa.year) as m3,
    (select amount from   aaa m where month=4   and m.year=aaa.year) as m4
from aaa   group by year
11、find_in_set笔记学习(历史文章有详解,此处略)
代码语言:javascript
复制
SELECT t.dept_id FROM sys_dept t 

WHERE FIND_IN_SET (102,ancestors) 
12、explain查看sql的具体的执行计划(历史文章有详解,此处略)
代码语言:javascript
复制
//微信公众号:关注挨踢小子
explain select * from tablename
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-08-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 挨踢小子 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、复制数据库表数据操作
  • 2、sql的子查询可以放到查询字段之中
  • 3、一张表中两个字段值互换(重点思想)
  • 4、实现数据库两列数据的上下移动操作
  • 5、mysql合并字符串
  • 6、删除重复数据,只保留最小ID编号
  • 7、两张关联表,删除主表中已经在副表中没有的信息
  • 8、日程安排提前五分钟提醒(日期操作)
  • 9、查询出每门课都大于90 分的学生姓名
  • 10、数据结构转换形式
  • 11、find_in_set笔记学习(历史文章有详解,此处略)
  • 12、explain查看sql的具体的执行计划(历史文章有详解,此处略)
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档