mysql查询语句复习小结
SQL查询语句基本语法:
select 字段列表
from 表名|视图列表
[where 条件表达式1]
[group by 属性名1 [having 条件表达式2]]
[order by 属性名2 [asc|desc]]
一、单表查询
1、使用*查询表中所有字段
eg:select * from student; -- 查询所有学生信息

2、查询指定字段
eg:select name,age from student; -- 查询所有学生的姓名和年龄

3、查询指定记录
使用where关键字,语法格式为:where 条件表达式
eg:select name 姓名,age 年龄 from student where id = 20150706801; -- 查询学号为20150706801的学生的姓名和年龄

二、关联查询
1、使用join关键字关联查询
①内连接(inner join) :连接两张表,连接条件使用on关键字,内连接只会显示匹配的数据记录
eg:select a.name 姓名,b.subject 科目,b.score 分数 from student a inner join score b on a.id = b.sid; -- 查询学生姓名、科目、分数

②外连接:外连接包括左连接和右连接
左连接(left join):返回左表中所有记录以及右表中符合连接条件的所有记录
eg:select a.name 姓名,b.subject 科目,b.score 分数 from student a left join score b on a.id = b.sid; -- 使用左连接查询学生姓名、科目、分数

右连接(right join):返回右表中所有记录以及左表中符合连接条件的所有记录
eg:select a.name 姓名,b.subject 科目,b.score 分数 from student a right join score b on a.id = b.sid; -- 使用右连接查询学生姓名、科目、分数

注:内外链接区别:内连接只会显示匹配的数据记录,外连接例如左连接会把左边表中所有记录显示出来,即使在右边表中没有匹配记录也会显示左表的数据,右连接反之。
2、使用表和表之间相同id关联查询
这种关联方式和内连接一样,只会显示出匹配的数据
eg:select a.name 姓名,b.subject 科目,b.score 分数 from student a,score b where a.id = b.sid;

三、使用聚合函数查询
count() 统计记录的条数
avg() 求平均值
sum() 求和
max() 求最大值
min() 求最小值
eg:select count(*) 学生总数 from student; -- 查询有多少学生
select avg(age) 平均年龄 from student; -- 查询学生平均年龄
select max(age) 最大年龄 from student; -- 查询学生的最大年龄
select min(age) 最小年龄 from student; -- 查询学生的最小年龄
四、子查询
eg:select st.name , sc.score from student st,score sc where st.id = sc.sid and sc.score = (select max(score) from score);-- 查询成绩最高的学生姓名和成绩

五、合并查询结果
使用union关键字合并查询结果,语法格式如下:
select 字段列表1 from table1 union [all] select 字段列表2 from table2
eg:select name 姓名,sex 性别,tel 电话 from student union select name,sex,phone from teacher;
注:union和union all的区别,union会去掉重复的记录,在结果集合并后悔对新产生的结果集进行排序运算,效率稍低,union all直接合并结果集,如果确定没有重复记录,建议使用union all
六、为表和字段添加别名
使用 as 关键字,as关键字可省略
1、给表添加别名
eg:select s.name,s.age from student as s where s.sex='男'; -- 查询所有男生的姓名和年龄
select s.name,s.age from student s where s.sex='男'; -- 查询所有男生的姓名和年龄
2、给字段添加别名
eg: select name as 姓名,age as 年龄 from student;
select name 姓名,age 年龄 from student; -- as可以省略
七、使用in关键字查询指定范围的记录
eg:select s.name,s.age,s.tel from student s where s.age in (20,21); -- 查询年龄为20或者21的学生姓名和电话
select s.name,s.age,s.tel from student s where s.age not in (20,21); -- 查询年龄不为20、21的学生姓名和电话
select s.name,s.age,s.tel from student s where s.age=20 or s.age=21; -- 查询年龄为20或者21的学生姓名和电话 in查询可以转换为用or查询
八、使用between...and...查询
eg:select s.name from student s,score sc where s.id=sc.sid and sc.score between 80 and 90; -- 查询分数在80分到90分之间的学生姓名
上句可以写成 select s.name from student s,score sc where s.id=sc.sid and sc.score >=80 and sc.score <=90;
九、模糊查询
通配符%:匹配任意长度字符,包括零字符
通配符_:匹配任意一个字符
eg:select s.name from student s where s.name like '张%'; -- 查询出所有姓张的同学

select s.name from student s where s.name like '_芷%'; -- 查询姓名中第二个字是芷的同学

十、查询空值
数据库里的空有两种情况:null或者是空字符串,如下图

eg:select s.name,s.tel from student s where tel is not null;
结果如下:
select s.name,s.tel from student s where tel is not null and tel =' ';
结果如下:
十一、使用and|or关键字多条件查询
eg:select s.name,s.age from student s where s.age=21 and s.sex = '女'; -- 查询年龄为21岁的女同学

select s.name,s.age,s.sex from student s where s.age=21 or s.sex = '女'; -- 查询年龄为21岁或者是性别为女的学生

十二、查询结果去掉重复记录
使用distinct关键字
eg:select distinct s.age from student; -- 查询学生的年龄并去掉重复记录
十三、对查询结果排序
使用order by来排序,语法格式:order by 排序字段 desc|asc
desc 降序,大—>小
asc 升序,小—>大,可省略不写,默认为升序
eg:select st.name,sc.subject,sc.score from score sc,student st where sc.sid = st.id order by sc.score desc; -- 查询学生成绩并按照成绩降序排列
十四、分组查询
使用group by进行分组,过滤条件使用having关键字
eg:select st.name 姓名,avg(sc.score) 平均分 from student st,score sc where st.id = sc.sid group by st.name having avg(sc.score)>70; -- 查询平均成绩在70分以上的学生姓名和平均成绩
十五、使用limit限制查询记录数量
eg:select * from student limit 5; -- 显示前5条
select * from student limit 2,5; -- 实际上查出来的是第3条到第6条
附:使用表如下:
学生表 student 
CREATE TABLE `student` (
`id` varchar(11) NOT NULL,
`name` varchar(5) DEFAULT NULL,
`sex` varchar(2) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`tel` varchar(11) DEFAULT NULL
);
成绩表 score 
CREATE TABLE `score` (
`sid` varchar(11) DEFAULT NULL,
`classid` varchar(3) DEFAULT NULL,
`subject` varchar(11) DEFAULT NULL,
`score` int(11) DEFAULT NULL
);
教师表 teacher 
CREATE TABLE `teacher` (
`id` varchar(3) DEFAULT NULL,
`classid` varchar(3) DEFAULT NULL,
`name` varchar(10) DEFAULT NULL,
`sex` varchar(4) DEFAULT NULL,
`phone` varchar(11) DEFAULT NULL
);
mysql查询语句复习小结的更多相关文章
- MySQL查询语句执行过程及性能优化(JOIN/ORDER BY)-图
http://blog.csdn.net/iefreer/article/details/12622097 MySQL查询语句执行过程及性能优化-查询过程及优化方法(JOIN/ORDER BY) 标签 ...
- mysql查询语句,通过limit来限制查询的行数。
mysql查询语句,通过limit来限制查询的行数. 例如: select name from usertb where age > 20 limit 0, 1; //限制从第一条开始,显示1条 ...
- MYSQL查询语句大全集锦
MYSQL查询语句大全集锦 1:使用SHOW语句找出在服务器上当前存在什么数据库: mysql> SHOW DATABASES; 2:2.创建一个数据库MYSQLDATA mysql> C ...
- MySQL查询语句执行过程及性能优化-基本概念和EXPLAIN语句简介
网站或服务的性能关键点很大程度在于数据库的设计(假设你选择了合适的语言开发框架)以及如何查询数据上. 我们知道MySQL的性能优化方法,一般有建立索引.规避复杂联合查询.设置冗余字段.建立中间表.查询 ...
- MySQL查询语句执行过程及性能优化-查询过程及优化方法(JOIN/ORDER BY)
在上一篇文章MySQL查询语句执行过程及性能优化-基本概念和EXPLAIN语句简介中介绍了EXPLAIN语句,并举了一个慢查询例子:
- mysql查询语句集
1. mysql 查询出某字段的值不为空的语句 1.不为空 select * from table where id <> ""; select * from tabl ...
- [转]MySQL查询语句执行过程详解
Mysql查询语句执行原理 数据库查询语句如何执行?语法分析:首先进行语法分析,对使用sql表示的查询进行语法分析,生成查询语法分析树.语义检查:检查sql中所涉及的对象以及是否在数据库中存在,用户是 ...
- Mysql查询语句中字符型字段不区分大小写解决方法
项目中和前端联调的时候,发现Mysql查询语句中字符型字段值过滤是不区分大小写的,之前没有关注过这个设置,特意去网上看了下,原因是Mysql中“COLLATE”属性区分大小写,而该属性默认值为“utf ...
- php面试专题---MYSQL查询语句优化
php面试专题---MYSQL查询语句优化 一.总结 一句话总结: mysql的性能优化包罗甚广: 索引优化,查询优化,查询缓存,服务器设置优化,操作系统和硬件优化,应用层面优化(web服务器,缓存) ...
随机推荐
- JS+PHP瀑布流效果
miai.php,代码如下: $link = mysql_connect("localhost","root",""); //连接数据库 $ ...
- Servlet 运行原理
一:servlet定义 Servlet是一个Java应用程序,运行在服务器端,用来处理客户端请求并作出响应的程序. 二:简单servlet实例 //导入所需的包 import javax.servle ...
- A Simple Web Server
介绍 在过去20几年里,网络已经在各个方面改变了我们的生活,但是它的核心却几乎没有什么改变.多数的系统依然遵循着Tim Berners-Lee在上个世纪发布的规则.大多数的web服务器都在用同样的方式 ...
- YAMLException: can not read a block mapping entry; a multiline key may not be an implicit key at line 5, column 1:
创建的md文件头部声明中没有加空格.
- 0405-服务注册与发现-客户端负载均衡-Ribbon 同Eureka使用,Ribbon脱离Eureka使用
一.Ribbon 同Eureka使用,注意事项 前几节一同使用,注意事项: 如果没有其他区域数据源,则根据客户端配置进行猜测(与实例配置相反).能够获取eureka.client.availabili ...
- 024-Spring Boot 应用的打包和部署
一.概述 二.手工打包[不推荐] 打包命令:maven clean package 打包并导出依赖:maven clean package dependency:copy-dependencies 1 ...
- R语言(一)
向量运算 R的强大功能之一就是把整个数据向量作为一个单一对象来处理.一个数据向量仅是数字的排列,一个向量可以通过如下方式构造 weight<-c(,,,) weight [] 结构c(--)用来 ...
- web.xml文件配置说明
web.xml作用: web.xml主要用来配置Filter.Listener.Servlet等,当我们去启动一个WEB项目时,容器(jetty.tomcat等)首先会读取项目web.xml配置文件里 ...
- pyhton3 hashlib模块
hashlib模块提供一下常量属性 hashlib.algorithms_guaranteed 获取保证在所有平台上此模块支持的哈希算法名称的集合 hashlib.algorithms_availab ...
- Java生成json
JSON(JavaScript Object Notation):一种轻量级的数据交换格式: Be JSON:在线JSON校验格式化工具 www.bejson.com 需求:编写代码生成如下的json ...