MySQL查询语句select讲解与练习-创新互联

select语句执行流程:

专注于为中小企业提供网站制作、成都网站设计服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业湖口免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了1000+企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

        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(默认)   降序:DESC LIMIT [[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;

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页名称:MySQL查询语句select讲解与练习-创新互联
本文网址:http://lszwz.com/article/shdoi.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款