mysql语句 学习笔记(二)
2014-05-21 19:01
477 查看
distinct--去除重复行
select distinct student_name,java_teacher
from student_table;
相等符号:= 单等号
不相等符号:<>
赋值运算符::= 冒号等号
expr1 between expr2 and expr3 表示expr2<=expr1<=expr3
exprt1 in(expr2,expr3,expr4..) 要求expr1是后面集合的其中一个元素
like 主要用于模糊查询 _匹配一个任意字符 %表示任意多个字符
eg. #查询学生表中学生名字以“刘”开头的学生信息
select * from student_table
where student_name like '刘%';
is null 要求指定值为null /*判断是否为空不要用null=null,因为SQL中该语句返回null*/
not 使用not对where条件取否
eg. select .......where not student_id>3;
desc 降序
asc 升序(默认)
eg. select * from student_table order by java_teacher desc;
转义字符
#查询名字以下画线开头的学生
mysql
select * from student_table
where student_name like '\_%';
标准SQL
select * from student_table
where student_name like '\_%' escape '\';
分组和组函数
avg count max min sum
group by
having
注意:1、where用于过滤行,having用于过滤组
2、不能在where子句中使用组函数,having子句才能使用组函数
limit 子句可以被用于强制 SELECT 语句返回指定的记录数。LIMIT 接受一个或两个数字参数。参数必须是一个
整数常量。如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大
数目。初始记录行的偏移量是 0(而不是 1):
eg. SELECT * FROM table LIMIT 5,10; // 检索记录行 6-15
多表连接查询
SQL92标准
等值连接
eg. select s.*,teacher_name
from student_table s ,teacher_table t
where s.java_teacher=t.teacher_id;
还需要对记录进行过滤,可用and将条件连接起来
非等值连接
eg. select s.*,teacher_name
from student_table s ,teacher_table t
where s.java_teacher>t.teacher_id;
外连接--就是在外连接符所在表中增加一个万能行,这一行的数据都是null,可以与另外一个表所有不满足条
件的记录进行匹配
eg. select s.*,teacher_name
from student_table s,teacher_table t
where s.java_teacher=t.teacher_id(*);
广义笛卡儿积--其实就是没有了where
eg. select s.*,teacher_name
from student_table s,teacher_table t;
自连接
子查询
eg.select *
from student_table
where java_teacher>
(select teacher_id
from teacher_table
where teacher_name='Yeeku');
如果子查询返回多个值,可用in,any,all in=any 表示任意一个
如果子查询返回多行多行,where子句应该有对应的数据列,并使用括号将多个数据列组合起来
eg.select *
from student_table
where (student_id,student_name)
=any(select teacher_id,teacher_name
from teacher_table);
集合运算
交intersect 并union 差minus
使用前提:两个结果集数据列的数量相等,且数据类型也一一对应。
mysql不支持intersect和minus
eg.select * from teacher_table
union
select student_id,student_name from student_table;
select distinct student_name,java_teacher
from student_table;
相等符号:= 单等号
不相等符号:<>
赋值运算符::= 冒号等号
expr1 between expr2 and expr3 表示expr2<=expr1<=expr3
exprt1 in(expr2,expr3,expr4..) 要求expr1是后面集合的其中一个元素
like 主要用于模糊查询 _匹配一个任意字符 %表示任意多个字符
eg. #查询学生表中学生名字以“刘”开头的学生信息
select * from student_table
where student_name like '刘%';
is null 要求指定值为null /*判断是否为空不要用null=null,因为SQL中该语句返回null*/
not 使用not对where条件取否
eg. select .......where not student_id>3;
desc 降序
asc 升序(默认)
eg. select * from student_table order by java_teacher desc;
转义字符
#查询名字以下画线开头的学生
mysql
select * from student_table
where student_name like '\_%';
标准SQL
select * from student_table
where student_name like '\_%' escape '\';
分组和组函数
avg count max min sum
group by
having
注意:1、where用于过滤行,having用于过滤组
2、不能在where子句中使用组函数,having子句才能使用组函数
limit 子句可以被用于强制 SELECT 语句返回指定的记录数。LIMIT 接受一个或两个数字参数。参数必须是一个
整数常量。如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大
数目。初始记录行的偏移量是 0(而不是 1):
eg. SELECT * FROM table LIMIT 5,10; // 检索记录行 6-15
多表连接查询
SQL92标准
等值连接
eg. select s.*,teacher_name
from student_table s ,teacher_table t
where s.java_teacher=t.teacher_id;
还需要对记录进行过滤,可用and将条件连接起来
非等值连接
eg. select s.*,teacher_name
from student_table s ,teacher_table t
where s.java_teacher>t.teacher_id;
外连接--就是在外连接符所在表中增加一个万能行,这一行的数据都是null,可以与另外一个表所有不满足条
件的记录进行匹配
eg. select s.*,teacher_name
from student_table s,teacher_table t
where s.java_teacher=t.teacher_id(*);
广义笛卡儿积--其实就是没有了where
eg. select s.*,teacher_name
from student_table s,teacher_table t;
自连接
子查询
eg.select *
from student_table
where java_teacher>
(select teacher_id
from teacher_table
where teacher_name='Yeeku');
如果子查询返回多个值,可用in,any,all in=any 表示任意一个
如果子查询返回多行多行,where子句应该有对应的数据列,并使用括号将多个数据列组合起来
eg.select *
from student_table
where (student_id,student_name)
=any(select teacher_id,teacher_name
from teacher_table);
集合运算
交intersect 并union 差minus
使用前提:两个结果集数据列的数量相等,且数据类型也一一对应。
mysql不支持intersect和minus
eg.select * from teacher_table
union
select student_id,student_name from student_table;
相关文章推荐
- MySql学习笔记(三)SQL简单入门语句--Fenby摘抄笔记
- MySQL学习笔记3SELECT 语句
- 【PHP+MySQL学习笔记】php操作MySQL数据库中语句
- MySQL基础语句【学习笔记】
- mysql常用语句(学习笔记)
- 我的Php学习笔记(六)Mysql简介和创建新的数据库 常用的SQL语句
- mysql学习笔记(常用语句)
- MySQL学习笔记-join语句类型
- MYSQL学习笔记——sql语句优化之索引
- MySQL学习笔记---基础概念和一些基础SQL语句(一)
- MySQL学习笔记 第六讲:select语句order by、limit、distinct、union
- MySql学习笔记3:where语句
- 马哥学习笔记十五——MySQL进阶之SQL语句
- MySQL学习笔记(10)之select查询语句
- MySQL学习笔记之基本语句
- mysql语句 学习笔记(一)
- MySQL基础语句【学习笔记】
- MySQL学习笔记 - 查询语句
- MySQL中的基本查询语句学习笔记
- mysql性能优化学习笔记(3)常见sql语句优化