您的位置:首页 > 数据库 > MySQL

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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mysql 数据库 sql select