serversql数据库的查询操作
sql数据库
*:first-child {
margin-top: 0 !important;
}
body>*:last-child {
margin-bottom: 0 !important;
}
/* BLOCKS
=============================================================================*/
p, blockquote, ul, ol, dl, table, pre {
margin: 15px 0;
}
/* HEADERS
=============================================================================*/
h1, h2, h3, h4, h5, h6 {
margin: 20px 0 10px;
padding: 0;
font-weight: bold;
-webkit-font-smoothing: antialiased;
}
h1 tt, h1 code, h2 tt, h2 code, h3 tt, h3 code, h4 tt, h4 code, h5 tt, h5 code, h6 tt, h6 code {
font-size: inherit;
}
h1 {
font-size: 28px;
color: #000;
}
h2 {
font-size: 24px;
border-bottom: 1px solid #ccc;
color: #000;
}
h3 {
font-size: 18px;
}
h4 {
font-size: 16px;
}
h5 {
font-size: 14px;
}
h6 {
color: #777;
font-size: 14px;
}
body>h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child {
margin-top: 0;
padding-top: 0;
}
a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
margin-top: 0;
padding-top: 0;
}
h1+p, h2+p, h3+p, h4+p, h5+p, h6+p {
margin-top: 10px;
}
/* LINKS
=============================================================================*/
a {
color: #4183C4;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
/* LISTS
=============================================================================*/
ul, ol {
padding-left: 30px;
}
ul li > :first-child,
ol li > :first-child,
ul li ul:first-of-type,
ol li ol:first-of-type,
ul li ol:first-of-type,
ol li ul:first-of-type {
margin-top: 0px;
}
ul ul, ul ol, ol ol, ol ul {
margin-bottom: 0;
}
dl {
padding: 0;
}
dl dt {
font-size: 14px;
font-weight: bold;
font-style: italic;
padding: 0;
margin: 15px 0 5px;
}
dl dt:first-child {
padding: 0;
}
dl dt>:first-child {
margin-top: 0px;
}
dl dt>:last-child {
margin-bottom: 0px;
}
dl dd {
margin: 0 0 15px;
padding: 0 15px;
}
dl dd>:first-child {
margin-top: 0px;
}
dl dd>:last-child {
margin-bottom: 0px;
}
/* CODE
=============================================================================*/
pre, code, tt {
font-size: 12px;
font-family: Consolas, "Liberation Mono", Courier, monospace;
}
code, tt {
margin: 0 0px;
padding: 0px 0px;
white-space: nowrap;
border: 1px solid #eaeaea;
background-color: #f8f8f8;
border-radius: 3px;
}
pre>code {
margin: 0;
padding: 0;
white-space: pre;
border: none;
background: transparent;
}
pre {
background-color: #f8f8f8;
border: 1px solid #ccc;
font-size: 13px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px;
}
pre code, pre tt {
background-color: transparent;
border: none;
}
kbd {
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
-moz-border-right-colors: none;
-moz-border-top-colors: none;
background-color: #DDDDDD;
background-image: linear-gradient(#F1F1F1, #DDDDDD);
background-repeat: repeat-x;
border-color: #DDDDDD #CCCCCC #CCCCCC #DDDDDD;
border-image: none;
border-radius: 2px 2px 2px 2px;
border-style: solid;
border-width: 1px;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
line-height: 10px;
padding: 1px 4px;
}
/* QUOTES
=============================================================================*/
blockquote {
border-left: 4px solid #DDD;
padding: 0 15px;
color: #777;
}
blockquote>:first-child {
margin-top: 0px;
}
blockquote>:last-child {
margin-bottom: 0px;
}
/* HORIZONTAL RULES
=============================================================================*/
hr {
clear: both;
margin: 15px 0;
height: 0px;
overflow: hidden;
border: none;
background: transparent;
border-bottom: 4px solid #ddd;
padding: 0;
}
/* TABLES
=============================================================================*/
table th {
font-weight: bold;
}
table th, table td {
border: 1px solid #ccc;
padding: 6px 13px;
}
table tr {
border-top: 1px solid #ccc;
background-color: #fff;
}
table tr:nth-child(2n) {
background-color: #f8f8f8;
}
/* IMAGES
=============================================================================*/
img {
max-width: 100%
}
-->
sql数据库的执行顺序
5. select 查看结果集中的哪个列,或列的计算结果
1. from 需要从哪个数据表检索数据
2. where 过滤表中数据的条件
3. group by 如何将上面过滤的数据分组
4. having 对已分组的数据进行过滤的条件
6. order by 按照不同排序方式返回数据
下述查询中所用的数据表
- 部门表dept数据
| did | dname | dmgr |
|---|---|---|
| 1 | 销售部 | 1001 |
| 2 | 研发部 | 1005 |
| 3 | 服务部 | 1008 |
- 员工表emp数据
| id | name | gender | age | salary | dno | mgrid |
|---|---|---|---|---|---|---|
| 1001 | 赵伟 | 男 | 38 | 6000 | 1 | |
| 1002 | 李萍兰 | 女 | 26 | 3000 | 1 | 1001 |
| 1003 | 秦晓 | 女 | 30 | 4000 | 1 | 1001 |
| 1004 | 赵晓辉 | 男 | 32 | 4200 | 1001 | |
| 1005 | 张岚 | 男 | 40 | 8000 | 2 | |
| 1006 | 李秋平 | 女 | 24 | 4000 | 2 | 1002 |
| 1007 | 顾世刚 | 男 | 30 | 6000 | 2 | 1002 |
| 1008 | 段大宇 | 男 | 36 | 5000 | 3 |
sql联表
- 内连接
- 指返回数据完全满足连接条件
- 外连接
- 指返回数据除了包含完全满足连接条件的数据,还包括不满足连接条件的数据
- 左外连接、右外连接、全连接
- 自连接
- 指连接的表是自身
- 交叉连接
- 表连接时没有连接条件,返回结果是两个表数据的笛卡尔积
内连接
- 连接语法一
select column_list
from table_name1,table_name2 #表之间通过逗号隔开
where codition #表之间的连接条件
- 连接语法二
select column_list
from table_name1
[inner] join table_name2 #表之间的连接通过 join实现
on condition; #表之间的连接条件
[...n]
外连接
外连接: 指表连接时除了返回符合连接条件的数据外,同时返回不符合连接条件的数据
- 左外连接:
- 是以左侧表为主,除返回符合连接条件的数据外,还返回左侧表不符合连接条件的数据
- 右外连接:
- 是以右侧表为主,除返回符合连接条件的数据外,还返回右侧表不符合连接条件的数据
全外连接:
- 除返回符合连接条件的数据外,还同时返回所有不符合连接条件的数据
语法:
select column_list
from table_name1
[left|right|full] [outer] join table_name2 #表之间的连接条件通过 xx join 实现
on condition #表之间的连接条件
[...n]
自连接
指对同一个表的连接,要执行一个内连接,必须使用不同的别名来区分
select e1.name 员工姓名,e2.name 上级主管
from emp e1,emp e2
where e1.mgrid=e2.id
交叉连接
在多表查询时,如果没有连接条件,则一个表的所有行将会和另一个表的所有行都会进行连接,连接的结果称为笛卡尔积
- 连接语法一:
select column_list
from table_name1,table_name2[...n]
- 连接语法二:
select column_list
from table_name1
cross join table_name2[...n]
子查询
在前面使用的查询语句都是只包含一条select语句,而有些情况下依靠单条select语句无法完成查询要求
这时候需要在select语句中内部嵌入另外的select语句,这条嵌入的select语句称为子查询
子查询除了可以应用到select语句中,也可以应用到insert、update、delete语句中
- 单行子查询
- 只返回一行一列数据的子查询称为单行子查询
- 外部查询可使用比较运算符: = 、> 、>= 、 < 、<= 、<> 操作符
- 多行子查询
- 返回多行单列数据的子查询称为多行子查询
- 外部查询可使用:in、not in 、any、all操作符
- 关联子查询
- 子查询引用外部查询中包含的一列或多列,查询的执行依赖外部查询
- 针对每行外部查询数据,都将执行一次子查询
- 外部查询可使用 exists、not exists 操作符
单行子查询
单行子查询不向外部的SQL语句返回结果,或者只返回一行
单行子查询可应用于select语句的where字句、having字句中
- 在where字句中使用
- 子查询作为条件判断的一方,位于小括号中(...)
- 查询年龄最小的员工信息
select * from emp
where age=(select min(age) from emp)
结果:查询到一条
- 查询大于最小年龄的员工信息
select * from emp
where age>(select min(age) from emp)
结果:查询到多条信息
- 在having字句中使用
- 查询部门员工平均年龄小于所有员工平均年龄的部门和该部门员工平均年龄
select dno 部门编号,avg(age) 平均年龄 from emp
group by dno
having avg(age)<(select avg(age) from emp)
多行子查询
多行子查询通常返回一条或多条记录,多行子查询结果用于where语句时
判断可以使用in、any、all操作符
-查询负责管理其他员工的员工信息 --in
select * from emp
where id in (select mgrid from emp where mgrid is not null)
- 查询年龄大于所有部门最大年龄的员工信息 --all
select * from emp
where age > all(select max(age) from emp group by dno)
- 查询年龄大于任意部门最大年龄的员工信息 --any
select * from emp
where age > any(select max(age) from emp group by dno)
关联子查询
子查询中引用父查询信息,称为关联子查询
关联子查询对于外部查询中的每一行都会运行一次
-查询每个部门年龄最小员工
select * from emp o
where age=(select min(age) from emp i where i.dno=o.dno)
-查询负责管理其他员工的员工信息
select * from emp e1
where exists (select 1 from emp e2 where e2.mgrid=e1.id)
-查询不负责管理其他员工的员工信息
select * from emp e1
where not exists (select 1 from emp e2 where e2.mgrid=e1.id)
高级查询
| 操作符 | 说明 |
|---|---|
| union all | 并运算。返回各个查询检索出的所有行,包括重复行 |
| union | 并运算。返回各个查询检索出的所有行,不包括重复行 |
| intersect | 交运算。返回两个查询检索的共有行 |
| except | 差运算。从左查询中返回右查询没有找到的所有的所有非重复值 |
union all 操作
union all 操作只合并结果集,不去除重复数据
- 查询所有产品
select * from prod1
union all
select * from prod2
结果可能含有重复行
union操作
union操作合并结果集后去除重复数据
-查询所有产品
select * from prod1
union
select * from prod2
结果:无重复行
intersect操作
intersect操作返回结果集间的交集,也就是共有部分
-查询两个产品表的共有产品
select * from prod1
intersect
select * from prod3
结果:返回两表共有行
serversql数据库的查询操作的更多相关文章
- 『动善时』JMeter基础 — 43、JMeter对数据库的查询操作
目录 1.使用"用户自定义变量"实现参数化 2. 在SQL Query中使用占位符传递参数 (1)传递的参数值是常量 (2)传递的参数值是变量 3.Variables names参 ...
- python数据库-MySQL数据库高级查询操作(51)
一.什么是关系? 1.分析:有这么一组数据关于学生的数据 学号.姓名.年龄.住址.成绩.学科.学科(语文.数学.英语) 我们应该怎么去设计储存这些数据呢? 2.先考虑第一范式:列不可在拆分原则 这里面 ...
- 【Query】使用java对mysql数据库进行查询操作
操作步骤: 1.加载数据库驱动(先在工程里加载数据库对应的驱动包) 2.获取连接 3.根据连接建立一个可执行sql的对象 4.执行sql语句 5.关闭连接 代码: package database; ...
- mybatis框架入门程序:演示通过mybatis实现数据库的查询操作
我们现在工程基于的数据库见“https://www.cnblogs.com/wyhluckdog/p/10147754.html”这篇博文. 1.mybatis下载 mybatis的代码由githua ...
- 5- MySQL数据库SELECT查询操作
复习: 数据类型: 数值型:整数(int,tinyint,smallint,bigint,mediumint) 浮点型(float ,double,decimal) 字符型:char(固定长度) ,v ...
- tp5 数据库Db查询操作
$data = Db::query('select * from tf_action'); $data = Db::query('select * from tf_action where id &g ...
- MySQL数据库学习笔记(九)----JDBC的ResultSet接口(查询操作)、PreparedStatement接口重构增删改查(含SQL注入的解释)
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
- mongodb_查询操作使用_条件查询、where子句等(转)
<?php /* mongodb_查询操作使用_条件查询.where子句等(转并学习) 1.find()/findOne() mongodb数据库的查询操作即使用find()或者findO ...
- Flask中Mysql数据库的常见操作
from flask import Flask,render_template #导入第三方链接库sql点金术 from flask_sqlalchemy import SQLAlchemy #建立对 ...
随机推荐
- Linux 文件夹压缩命令总结
tar命令 解包:tar zxvf FileName.tar 打包:tar czvf FileName.tar DirName gz命令 解压1:gunzip FileName.gz 解压2:gzip ...
- .NET Core实战项目之CMS 第十三章 开发篇-在MVC项目结构介绍及应用第三方UI
作为后端开发的我来说,前端表示真心玩不转,你如果让我微调一个位置的样式的话还行,但是让我写一个很漂亮的后台的话,真心做不到,所以我一般会选择套用一些开源UI模板来进行系统UI的设计.那如何套用呢?今天 ...
- RedirectToAction()转移方式及参数传递
今天在做一个功能的时,使用RedirectToAction()需要从这里传几个参数,从网上查了一下,这样解决.真好. Return RedirectToAction("Index" ...
- qt之窗口换肤
1.相关文章 Qt 资源系统qt的moc,uic,rcc命令的使用 2.概要 毕业两年了,一直使用的是qt界面库来开发程序,使用过vs08.10.13等开发工具,并安装了qt的插件,最近在做客户 ...
- 『离散化 discrete』
离散化(discrete) 离散化可以说是一个很基础的算法吧,但是有些时候还是很好用很有必要的算法. 离散化的排序的一个运用,具体地讲,离散化算法是将无穷大集合中的若干个元素映射到有限大小的集合中,以 ...
- 并发编程(五)——AbstractQueuedSynchronizer 之 ReentrantLock源码分析
本文将从 ReentrantLock 的公平锁源码出发,分析下 AbstractQueuedSynchronizer 这个类是怎么工作的,希望能给大家提供一些简单的帮助. AQS 结构 先来看看 AQ ...
- .NET Core中的路由约束
背景介绍 上周给大家分享了Nancy in .NET Core学习笔记 - 路由之后, 就一直在考虑.NET Core能否实现和Nancy中一样的路由约束, 最近查阅了一下MSDN及一些国外博客, 发 ...
- Quartz.NET学习笔记(三) 简单触发器
触发器是Quartz.NET的另外第一个核心元素,他有2种类型,简单触发器(Simple Trigger)和计划任务触发器(Cron Trigger), 一个触发器可以绑定一个任务. 通用触发器属性 ...
- shell之最常用的服务脚本
任务需求:以最简单的方式管理 /usr/local/php7/sbin/php-fpm -c /usr/local/php7/etc/php.ini 这条命令 包括启动停止重启 使用技术:shell脚 ...
- 重复造轮子,编写一个轻量级的异步写日志的实用工具类(LogAsyncWriter)
一说到写日志,大家可能推荐一堆的开源日志框架,如:Log4Net.NLog,这些日志框架确实也不错,比较强大也比较灵活,但也正因为又强大又灵活,导致我们使用他们时需要引用一些DLL,同时还要学习各种用 ...