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 查询方法 统计查询 链接查询 子查询的更多相关文章

  1. Mysql高手系列 - 第12篇:子查询详解

    这是Mysql系列第12篇. 环境:mysql5.7.25,cmd命令中进行演示. 本章节非常重要. 子查询 出现在select语句中的select语句,称为子查询或内查询. 外部的select查询语 ...

  2. MySQL开发——【联合查询、多表连接、子查询】

    联合查询 所谓的联合查询就是将满足条件的结果进行拼接在同一张表中. 基本语法: select */字段 from 数据表1 union [all | distinct] select */字段 fro ...

  3. (五)连接查询(SQL99标准)、子查询、分页查询、联合查询

    一.连接查询(SQL99标准) 1.含义:当要查询的数据来自多张表时要使用连接查询 2.语法: select 查询列表 from 表1 别名 [连接类型] join 表2 别名 on 连接条件 [wh ...

  4. MySQL数据库基础(四)(子查询与链接)

    1.子查询简介 其中,所谓的"外层查询"并不是指"查找",指的是所有SQL语句的统称:结构化查询语言(Structured Query Language),简称 ...

  5. MySQL学习笔记——多表连接和子查询

    多表连接查询 # 返回的是两张表的乘积 SELECT * FROM tb_emp,tb_dept SELECT COUNT(*) FROM tb_emp,tb_dept # 标准写法,每个数据库都能这 ...

  6. Mysql 多表数据拼接插入及子查询结果集随机取一条

    最近遇到一个测试数据的需求,需要往一个表中插入4个来源的数据. 往orders 表中插入 来自 sql_person cm_user_car_model cm_sp_product_new 部分固定数 ...

  7. 【MySQL】-2 函数、分组、子查询、联合查询

    函数 Mysql的函数特性没有SQL可移植性强. 大多数情况下支持的函数: 处理文本串的函数: RTrim():处理列值右边的空格 LTrim():处理列值左边的空格   Trim():处理列值的左右 ...

  8. 【MySQL】MySQL/MariaDB的优化器对in子查询的处理

    参考:http://codingstandards.iteye.com/blog/1344833 上面参考文章中<高性能MySQL>第四章第四节在第三版中我对应章节是第六章第五节 最近分析 ...

  9. MySQL数据库(10)----IN 和 NOT IN 子查询

    当子查询要返回多个行来与外层查询进行比较运算时,可以使用运算符 IN 和 NOT IN.它们会测试某个给定的比较值是否存在于某一组值里.如果外层查询里的行与子查询返回的某一个行相匹配,那么 IN 的结 ...

随机推荐

  1. Cordova打包vue项目(Android)

    准备工作: 安装好必要环境: vue-cli,webpack,node.js,android环境 (http://cordova.axuer.com/docs/zh-cn/latest/guide/p ...

  2. mac系统下安装mysql步骤

    1.下载mysql-5.7.13-osx10.11-x86_64.dmg安装包,并点击dmg安装包进行安装 2.安装完成后弹出如以下提示信息: 2016-06-23T01:14:48.649253Z ...

  3. I.MX6 dhcpcd 需要指定网卡

    /************************************************************************** * I.MX6 dhcpcd 需要指定网卡 * ...

  4. Ordeby then by

    先按orderby排序,再按thenby排序 return PartialView("_ClickRangeOnCategory", articles.OrderByDescend ...

  5. Visual Studio一些插件

    让JavaScript像C#一样支持Region http://www.cnblogs.com/codealone/p/3647127.html

  6. 基于ssh框架web示例

    基于ssh框架web示例 介绍 Spring Boot Web 开发非常简单,该示例包括包括目前web开发基本都需要用到的内容 - 序列化(json)输出 - 过滤器(filters) - 监视器(l ...

  7. SQL 初级教程学习(六)

    1.创建视图 CREATE VIEW [Current Product List] ASSELECT ProductID,ProductNameFROM ProductsWHERE Discontin ...

  8. Android上的线程安全

    Thread-safe methods In some situations, the methods you implement might be called from more than one ...

  9. 转 Oracle Transportable TableSpace(TTS) 传输表空间 说明

    ############1   迁移数据库的集中方法 三.相关技术 迁移方式 优势 不足1 Export and import • 对数据库版本,以及系统平台没有要求 • 不支持并发,速度慢• 停机时 ...

  10. 定时清除 /var/log/massage 下的信息脚本文件

    定时清除 /var/log/massage 下的信息脚本 #!/bin/sh #Date: 0:07 #Author: Xiaodong #Mail: 990974238@qq.com #Puncti ...