MYSQL 查询方法 统计查询 链接查询 子查询
mysql表格查询方法:
查询:
1.简单查询
select * from Info --查所有数据
select Code,Name from Info --查指定列的数据
select Code as '代号',Name as '姓名' from Info --给列指定别名
2.条件查询
select * from Info where Code='p001'
select * from Info where Sex='true' and Nation='n001' --多条件并的关系
select * from Info where Sex='true' or Nation='n001' --多条件或的关系
3.范围查询
select * from Car where Price>40 and Price<50
select * from Car where Price between 40 and 50
4.离散查询
select * from Car where Code in ('c001','c005','c010','c015')
select * from Car where Code not in ('c001','c005','c010','c015')
5.模糊查询
select * from Car where Name like '%宝马%' --查包含宝马的
select * from Car where Name like '宝马%' --查以宝马开头的
select * from Car where Name like '%宝马' --查以宝马结尾的
select * from Car where Name like '宝马' --查等于宝马的
select * from Car where Name like '__E%' --查第三个字符是E的
% 代表是任意多个字符
_ 代表是一个字符
6.排序查询
select * from Car order by Price asc --以价格升序排列
select * from Car order by Price desc --以价格降序排列
select * from Car order by Oil desc,Price asc --以两个字段排序,前面的是主条件后面的是次要条件
7.分页查询
select top 5 * from Car
select top 5 * from Car where Code not in (select top 5 Code from Car)
当前页:page = 2; 每页显示:row = 10;
select top row * from Car where Code not in (select top (page-1)*row Code from Car)
8.去重查询
select distinct Brand from Car
9.分组查询
select Brand from Car group by Brand having count(*)>2
10.聚合函数(统计查询)
select count(*) from Car --查询所有数据条数
select count(Code) from Car --查询所有数据条数
select sum(Price) from Car --求和
select avg(Price) from Car --求平均
select max(Price) from Car --求最大值
select min(Price) from Car --求最小值
高级查询
1.连接查询
select * from Info,Nation --形成笛卡尔积
select * from Info,Nation where Info.Nation = Nation.Code
select Info.Code,Info.Name,Sex,Nation.Name,Birthday from Info,Nation where Info.Nation = Nation.Code
select * from Info join Nation on Info.Nation = Nation.Code --join on 的形式
2.联合查询
select Code,Name from Info
union
select Code,Name from Nation
3.子查询
一条SQL语句中包含两个查询,其中一个是父查询(外层查询),另一个是子查询(里层查询),子查询查询的结果作为父查询的条件。
--查询民族为汉族的所有人员信息
select * from Info where Nation = (select Code from Nation where Name = '汉族')
(1)无关子查询
子查询可以单独执行,子查询和父查询没有一定的关系
--查询系列是宝马5系的所有汽车信息
select * from Car where Brand =(select Brand_Code from Brand where Brand_Name = '宝马5系')
(2)相关子查询
--查找油耗低于该系列平均油耗的汽车
select * from Car where Oil<(该系列的平均油耗)
select avg(Oil) from Car where Brand = (该系列)
select * from Car a where Oil<(select avg(Oil) from Car b where b.Brand = a.Brand)
MYSQL 查询方法 统计查询 链接查询 子查询的更多相关文章
- Mysql高手系列 - 第12篇:子查询详解
这是Mysql系列第12篇. 环境:mysql5.7.25,cmd命令中进行演示. 本章节非常重要. 子查询 出现在select语句中的select语句,称为子查询或内查询. 外部的select查询语 ...
- MySQL开发——【联合查询、多表连接、子查询】
联合查询 所谓的联合查询就是将满足条件的结果进行拼接在同一张表中. 基本语法: select */字段 from 数据表1 union [all | distinct] select */字段 fro ...
- (五)连接查询(SQL99标准)、子查询、分页查询、联合查询
一.连接查询(SQL99标准) 1.含义:当要查询的数据来自多张表时要使用连接查询 2.语法: select 查询列表 from 表1 别名 [连接类型] join 表2 别名 on 连接条件 [wh ...
- MySQL数据库基础(四)(子查询与链接)
1.子查询简介 其中,所谓的"外层查询"并不是指"查找",指的是所有SQL语句的统称:结构化查询语言(Structured Query Language),简称 ...
- MySQL学习笔记——多表连接和子查询
多表连接查询 # 返回的是两张表的乘积 SELECT * FROM tb_emp,tb_dept SELECT COUNT(*) FROM tb_emp,tb_dept # 标准写法,每个数据库都能这 ...
- Mysql 多表数据拼接插入及子查询结果集随机取一条
最近遇到一个测试数据的需求,需要往一个表中插入4个来源的数据. 往orders 表中插入 来自 sql_person cm_user_car_model cm_sp_product_new 部分固定数 ...
- 【MySQL】-2 函数、分组、子查询、联合查询
函数 Mysql的函数特性没有SQL可移植性强. 大多数情况下支持的函数: 处理文本串的函数: RTrim():处理列值右边的空格 LTrim():处理列值左边的空格 Trim():处理列值的左右 ...
- 【MySQL】MySQL/MariaDB的优化器对in子查询的处理
参考:http://codingstandards.iteye.com/blog/1344833 上面参考文章中<高性能MySQL>第四章第四节在第三版中我对应章节是第六章第五节 最近分析 ...
- MySQL数据库(10)----IN 和 NOT IN 子查询
当子查询要返回多个行来与外层查询进行比较运算时,可以使用运算符 IN 和 NOT IN.它们会测试某个给定的比较值是否存在于某一组值里.如果外层查询里的行与子查询返回的某一个行相匹配,那么 IN 的结 ...
随机推荐
- Object Query Language (OQL) query 基本使用
OQL是对java堆内存进行查询分析 使用方法: 1.查看java线程id jps -lvm 上图红框中的为pid: 2.运行命令-导出堆内存信息-会生成一份文件在你本地 jmap -dump:liv ...
- JS数组array常用方法
JS数组array常用方法 1.检测数组 1)检测对象是否为数组,使用instanceof 操作符 if(value instanceof Array) { //对数组执行某些操作 } 2)获取对象的 ...
- SPFA 算法详解( 强大图解,不会都难!) (转)
适用范围:给定的图存在负权边,这时类似Dijkstra等算法便没有了用武之地,而Bellman-Ford算法的复杂度又过高,SPFA算法便 派上用场了. 我们约定有向加权图G不存在负权回路,即最短路径 ...
- mac系统下如何删除银行安全插件
要分类解决了,一般safair插件都是pkg包安装的 如果:1.制作者够良心,PKG安装包中植入了删除卸载功能,那就好办了,打开当时安装的pkg包,执行删除选项 2.没良心的,装了不好删的,我目前个人 ...
- 【FFT初识】
FFT在用于解决多项式乘法A*B(A和B为多项式,形如a0+a1*x^1+a2*x^2....)的时候,通俗地解释就是: 原理:先根据各自的系数各自转化为对应的向量(O(nlogn)),然后向量相 ...
- BZOJ_3448_[Usaco2014 Feb]Auto-complete_Trie树
BZOJ_3448_[Usaco2014 Feb]Auto-complete_Trie Description Bessie the cow has a new cell phone and enjo ...
- [Codeforces 839C] Journey
[题目链接] http://codeforces.com/contest/839/problem/C [算法] 概率DP 时间复杂度 : O(N) [代码] #include<bits/stdc ...
- UVa 1161 Objective: Berlin (最大流)
题意:给定一些航班,每个航班有人数,和起始终止时间,每次转机要花半小时,问限制时间内最多能有多少人从起始城市到终点城市. 析:差不多是裸板网络流的最大流问题,把每个航班都拆成两个点,这两个点之间连接一 ...
- Find offset of first/last found substring
DATA: TEXT TYPE STRING VALUE 'RO_LL_OVER BEET_HOVEN', MOFF TYPE I VALUE '-1'. FIND FIRST OCCURRENCE ...
- 【POJ - 2251】Dungeon Master (bfs+优先队列)
Dungeon Master Descriptions: You are trapped in a 3D dungeon and need to find the quickest way out! ...