赞
踩
使用where子句指定查询条件
语法格式:
select 字段
from 表名
where表达式;
= <= = != <> !<(>=) !>(<=)
例1:查询sc表里成绩大于80分的学生的学号,课程以及所对应的成绩;
select sno,cno,degree from sc where degree>80;
例2:查询sc表里成绩小于60分的学生的学号,课程以及所对应的成绩;
select sno,cno,degree from sc where degree<60;
例3:查询系别不为信息工程的学生的学号、姓名、系别;
select sno,sname,sdept from student where sdept != "信息工程";
多条件连接时使用 NOT(非:不是) AND(且) OR(或)
and 和or一块使用,and的优先级高于or
1、查询1、出生日期在1988年以后的2、男生的信息(AND)
and:字面上理解:且的意思;逻辑理解:多个条件,每一个条件每一个都需要满足;
select * from student where year(sbirthday)>1988 and ssex="男";
2、sc表查询成绩在95以上或60以下的学号和成绩
or:字面上理解:或的意思;逻辑判断:多个条件里面,满足其中一个条件即可;
select sno,degree from sc where degree>95 or degree<60;
区别于
select sno,degree from sc where degree>95 and degree<60;
3、查询非1988年出生的学生信息(not 或者!=)
select * from student where not year(sbirthday)=1988;
select * from student where year(sbirthday)!=1988;
4、查询信息工程和电子工程的学生的信息(注意)
sdept:or
多个字段有限制:and
sdept=“信息工程” or sdept=“电子工程”;
select sno,degree from sc where sdept="信息工程" or sdept="电子工程";
Between and not Between and
1、查询成绩在60~70分的学号和成绩(>=60 and <=70)
select 字段 from 表名 where 字段 between 左范围(范围里面的最小值)and 右范围(范围里面的最大值)
错误写法:between 最大值 and 最小值;
正确写法:between 最小值 and 最大值;
(等价)
(1)select sno,degree from sc where degree between 60 and 70;
(2)select sno,degree from sc where degree>=60 and degree<=70;
2、出生年份在1988-1989之间的学生信息
(1)select * from student where year(sbirthday) between 1988 and 1989;
(2)select * from student where year(sbirthday) >=1988 and year(sbirthday)<=1989;
情景:
(1)在student里查找姓张的同学;(没有明确姓名)
(2)查找学号里面包含6的学生的信息;(明确学号)
(模糊查询:没有给定精确的查询条件)提到模糊查询联想到like和匹配运算符
(一)匹配任意多个字符用’%’
%M:任意字符串,以‘M’结尾的
M%:任意字符串,以‘M’开头的
%M%:任意字符串,包含’M’的.
(二)匹配任意一个字符用’_’
M:任意两个字符,‘M’结尾
M:任意两个字符,‘M’开头
M:任意三个字符,中间是’M’
1、查询所有姓“李”的同学的信息
select * from student where sname like "李%";
2、查询所有不姓“李”的同学的信息(not like)
select * from student where sname not like "李%";
3、查询所有姓“王”的和姓“张”的同学的信息
(注意后一个字段 like不能省略)
select * from student where sname like "王%" or sname like "张%";
4、查询姓名中第三个字是“伟”的同学的信息。
select * from student where sname like "__伟%";
某查询条件的取值在某个列表中
1、查询信息工程系、软件工程系、计算机工程系学生的姓名和性别。
系别在(信息工程系、软件工程系、计算机工程系)列表中
Where sdept in(信息工程系、软件工程系、计算机工程系) 或
Where sdept=’’ or sdept =’’ or sdept=’’
select ssex,sname from student where sdept in(信息工程系、软件工程系、计算机工程系);
select ssex,sname from student where sdept=’信息工程系’ or sdept =’软件工程系’ or sdept=’计算机工程系’;
2、查询不是信息工程系、软件工程系、计算机工程系学生的姓名和性别。
Not in
select ssex,sname from student where sdept not in(信息工程系、软件工程系、计算机工程系);
1、student表里查询系别为信息工程系的女生的相关信息;
(and且)
select * from student where sdept="信息工程系" and ssex="女";
2、student表里查询不是6月份出生的人的相关信息;
(1)select * from student where month(sbirthday)!=6;
(2)select * from student where date_format(sbirthday,"%m")!=6;
3、查询sc表里成绩不高于70分的学生的相关信息;(<=)
select * from sc where degree<=70;
4、查询sc表里c02课程成绩为80分的学生的相关信息;
select * from sc where cno="c02" and degree=80;
5、查询student表年龄在33-40之间的学生的相关信息;(范围运算符:between and)
1、
select * from student where year(now())-year(sbirhday) (date_format(sbirthday,"%m%d")>date_format(now(),"%m%d")) between 33 and 40;
2、timestampdiff函数;
select * from student where timestampdiff(year,sbirthday,now()) between 33 and 40;
6、查询student表里学号最后一位为8的学生的相关信息;
模糊:like,%匹配任意多个字符,_任意单个字符
select * from student where sno like "%8";
7、查询student表里学号第4位为6的学生的相关信息;
(修改题目为:第八位为7)
select sno from student where sno like "_______7%";
正则匹配:
select sno from student where sno rlike "^.{7}7";
(1)select * from student where sno like “___6%”;
(2)正则匹配:select * from student sno rlike “^…6”;
like:_任意一个字符
正则匹配:.任意一个字符
^ 匹配搜索字符串开头处的位置
8、查询teacher表里信息工程系、计算机工程系、软件工程系的老师的信息;
in 指定查询条件的取值如果是一个列表的话:字段in (列表);
select * from teacher where tdept in("信息工程系","计算机工程系","软件工程系");
9、查询teacher表里不是信息工程系、计算机工程系、软件工程系的男老师
select * from teacher where tdept!="信息工程系" and tdept!="计算机工程系" and tdept!="软件工程系" and ssex="男";
select * from teacher where tdept not in("信息工程系","计算机工程系","软件工程系") and tsex="男";
timestampdiff的用法:求时间差:
TIMESTAMPDIFF(单位,开始时间,结束时间)
单位可以选用以下参数:
SECOND。秒TIMESTAMPDIFF(hour,开始时间,结束时间)
MINUTE。分钟
HOUR。小时
DAY。天
WEEK。星期
MONTH。月
QUARTER。季度
YEAR。年
SQLyog:常用的两个快捷方式
Ctrl+Shift+C 注释 SQL 窗口选择内容
Ctrl+Shift+R 从选择内容删除注释
单表有条件查询
查询条件 运算符
比较运算符 =,<,>,<=,>=,<>,!=,!<,!>
逻辑运算符 AND,OR,NOT
范围运算符 BETWEEN AND,NOT BETWEEN AND
列表运算符 IN,NOT IN
字符匹配符 LIKE,NOT LIKE
空值 IS NULL,IS NOT NULL
小练:
1、查询student表里的sphone字段,要求sphone不为null且不能为空,并按照号码升序排列;
2、查询student表里sno,sname字段,只查询年龄最大的前十个同学;
3、统计sc表里每个学生的平均成绩,并按照平均分降序排列;
4、统计sc表里每个学生的平均成绩,要求平均成绩超过80分,并按照平均分升序排列;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。