千家信息网

MySQL 查询语句select讲解与练习

发表于:2024-09-22 作者:千家信息网编辑
千家信息网最后更新 2024年09月22日,select语句执行流程:START------>1.FROM------>2.WHERE(选择,合适的行)------>3.GROUP BY(分组)------>4.HAVING(对分组进行过滤)
千家信息网最后更新 2024年09月22日MySQL 查询语句select讲解与练习

select语句执行流程:

START------>1.FROM

------>2.WHERE(选择,合适的行)

------>3.GROUP BY(分组)

------>4.HAVING(对分组进行过滤)

------>5.ORDER BY(排序)

------>6.SELECT(投影,合适的字段)

------>7.LIMIT ------>end result


select单表查询:


关键字:

DISTINCT        #数据去重      例:select DISTINCT gender from students;VARIABLES       #mysql服务器自身内置变量      例:select variables like 'query%';AS              #显示时使用别名       例:select name as  stuname  from  students;IN                  例:select name,age from students where age in  (18,19,25);IS NULL         #取值为空,IS NOT NULL: 取值不为空like            #%任意长度任意字符  _任意单个字符RLIKE           #使用正则表达式GROUP           #根据指定的条件把查询结果进行分组以用于做聚合运算内置函数:avg() , max() , min() , count() , sum()order  by       #根据指定字段对查询结果进行排序                  升序:ASC(默认)   降序:DESCLIMIT [[offset,]row_count] #对查询的结果进行输出行数数量的限制对查询结果中的数据请求施加'锁':       FOR  UPDATE :写锁,独占锁,排他锁       LOCK IN SHARE MODE :读锁,共享锁


例:查看男女同学的平均年龄

       select avg(age),gender  from  students  group  by  gender ;

例:查看平均年龄大于20的性别

       select avg(age),gender as '年龄' from  students  group  by  gender having 年龄>20;

例:查看姓名,年龄以年龄倒序排序

       select name,age from students order  by age desc;

例:年龄从小到大查看排名11至20的同学的姓名

select name,age from students order  by age limit 10,10 ;


练习题

1. 在students表中,年龄大于25,且为男性的同学的姓名和年龄

select name,age  from students  where gender='m' and age>25;

2. 以classID为分组依据,显示每组的平均年龄

select  avg(age),classID from students where classID is not null group by classID;

3. 显示第二题中平均年龄大于30的分组及平均年龄

select  avg(age),classID from students  group by classID  having avg(age)>30;

4. 显示名字以L开头的同学的相关信息

select * from students  where name like  'L%';

5. 显示teacherID非空的同学的相关信息

select * from students  where teacherID is not null;

6. 以年龄排序后显示年龄最大的前10位同学的信息

select * from students  order by  age DESC limit 10;

7. 查询年龄大于等于20岁,小于等于25岁的同学的信息,用三种方法

select * from students where age>=20 and age<=25;select * from students  where age between 20 and  25;select * from students  where age in (20,21,22,23,24,25);


select多表查询:


交叉连接:又称笛卡尔乘积,结果两表行数相乘(不常用)。 例:select *  from  table1,table2;内连接:  等值连接求交集,让两张或多张表按"等值"建立连接关系(常用)外连接:  又分左连接(显示所有左边给定所有字段和右边与左边指定字段内容相同的),右连接。


例:

select * from students,teachers  where  students.teacherID=teachers.TID ; #相当于内连接select s.name,c.class from students as s,classes as c  where s.classID=c.classID;

不等值连接:

自然连接:

自连接:一张表中一个字段的值等于另一个字段的值。

例:

select  s.name,t.name  from students  as  s,teacher  as t where  s.TeacherID=t.stuID;

外连接:

左外连接:以左侧表为准,以某一字段等值建立连接关系,如左表有的右表也有就一一对应,如左表有右表没有左表显示所有,右表留空对应。(显示左表所有,右表有的就对应没有就留空)

使用方法:FROM tb1  LEFT  JOIN  tab2  ON  tab1.col1=tab2.col;

例:

select s.name,c.class  from students  as s  LEFT  JOIN  classes  as  c  ON  s.classID=c.classID;

右外连接:

使用方法:FROM tb1  RIGHT  JOIN  tab2  ON  tab1.col1=tab2.col

子查询:在查询语句中嵌套着查询语句(mysql支持不好,少用)

基于某语句结果再次进行查询

用在where子句中的子查询:

(1) 用在比较表达式中的子查询,子查询仅能返回单个值:

例查找大于平均年龄的同学名字和年龄:

select name,age  from  students  where  age>(select avg(age) from students);

(2)用在IN中的子查询:子查询应该单键查询并返回一个或多个值构成列表

例:查找老师年龄和同学年龄相等的

select  name,age  from students where age  in (select age from teachers);

(3)用于EXISTS

用于from子句中的子查询:

例查找平均年龄是30的班级:

select s.aage,s.classID from (select avg(age) as aage,classID from students where classID is not null group by classID) as s where s.aage=30;

联合查询:把两个表查询的结果合并成一个。以前面表的字段为准,后面的表填充内容。

例:

select name,age  from  students  UNION select  name,age  from teachers;


0