MySQL 查询语句使用进阶
===============================================================================
概述:
===============================================================================
练习:
练习1
首先导入hellodb.sql的脚本文件,查询其数据库和表如下:
[root@centos7 ~]# mysql -p134296 < hellodb.sql # 导入数据库脚本文件; [root@centos7 ~]# mysql -p134296 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 6 Server version: 5.5.44-MariaDB MariaDB Server Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | Syslog | | hellodb | # 生成的hellodb数据库 | mydb | | mysql | | performance_schema | | test | | testdb | | ultrax | | zabbix | +--------------------+ 10 rows in set (0.01 sec) MariaDB [(none)]> use hellodb; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed MariaDB [hellodb]> show tables; # 查看hellodb数据库中的表如下: +-------------------+ | Tables_in_hellodb | +-------------------+ | classes | | coc | | courses | | scores | | students | | teachers | | toc | +-------------------+ 7 rows in set (0.00 sec)
(1) 在students表中,查询年龄大于25岁,且为男性的同学的名字和年龄;
MariaDB [hellodb]> desc students; +-----------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------+---------------------+------+-----+---------+----------------+ | StuID | int(10) unsigned | NO | PRI | NULL | auto_increment | | Name | varchar(50) | NO | | NULL | | | Age | tinyint(3) unsigned | NO | | NULL | | | Gender | enum(‘F‘,‘M‘) | NO | | NULL | | | ClassID | tinyint(3) unsigned | YES | | NULL | | | TeacherID | int(10) unsigned | YES | | NULL | | +-----------+---------------------+------+-----+---------+----------------+ 6 rows in set (0.01 sec) MariaDB [hellodb]> select * from students; +-------+---------------+-----+--------+---------+-----------+ | StuID | Name | Age | Gender | ClassID | TeacherID | +-------+---------------+-----+--------+---------+-----------+ | 1 | Shi Zhongyu | 22 | M | 2 | 3 | | 2 | Shi Potian | 22 | M | 1 | 7 | | 3 | Xie Yanke | 53 | M | 2 | 16 | | 4 | Ding Dian | 32 | M | 4 | 4 | | 5 | Yu Yutong | 26 | M | 3 | 1 | | 6 | Shi Qing | 46 | M | 5 | NULL | | 7 | Xi Ren | 19 | F | 3 | NULL | | 8 | Lin Daiyu | 17 | F | 7 | NULL | | 9 | Ren Yingying | 20 | F | 6 | NULL | | 10 | Yue Lingshan | 19 | F | 3 | NULL | | 11 | Yuan Chengzhi | 23 | M | 6 | NULL | | 12 | Wen Qingqing | 19 | F | 1 | NULL | | 13 | Tian Boguang | 33 | M | 2 | NULL | | 14 | Lu Wushuang | 17 | F | 3 | NULL | | 15 | Duan Yu | 19 | M | 4 | NULL | | 16 | Xu Zhu | 21 | M | 1 | NULL | | 17 | Lin Chong | 25 | M | 4 | NULL | | 18 | Hua Rong | 23 | M | 7 | NULL | | 19 | Xue Baochai | 18 | F | 6 | NULL | | 20 | Diao Chan | 19 | F | 7 | NULL | | 21 | Huang Yueying | 22 | F | 6 | NULL | | 22 | Xiao Qiao | 20 | F | 1 | NULL | | 23 | Ma Chao | 23 | M | 4 | NULL | | 24 | Xu Xian | 27 | M | NULL | NULL | | 25 | Sun Dasheng | 100 | M | NULL | NULL | +-------+---------------+-----+--------+---------+-----------+ 25 rows in set (0.00 sec) MariaDB [hellodb]> select Name,Age,Gender from students where Age > 25 and Gender = ‘M‘; +--------------+-----+--------+ | Name | Age | Gender | +--------------+-----+--------+ | Xie Yanke | 53 | M | | Ding Dian | 32 | M | | Yu Yutong | 26 | M | | Shi Qing | 46 | M | | Tian Boguang | 33 | M | | Xu Xian | 27 | M | | Sun Dasheng | 100 | M | +--------------+-----+--------+
(2) 以ClassID为分组依据,显示每组的平均年龄;
MariaDB [hellodb]> select ClassID,avg(age) as avg_age from students group by ClassID; +---------+---------+ | ClassID | avg_age | +---------+---------+ | NULL | 63.5000 | | 1 | 20.5000 | | 2 | 36.0000 | | 3 | 20.2500 | | 4 | 24.7500 | | 5 | 46.0000 | | 6 | 20.7500 | | 7 | 19.6667 | +---------+---------+ 8 rows in set (0.01 sec)
(3) 显示第2题中平均年龄大于30的分组及平均年龄;
MariaDB [hellodb]> select ClassID,avg(age) as avg_age from students group by ClassID having avg_age > 30; +---------+---------+ | ClassID | avg_age | +---------+---------+ | NULL | 63.5000 | | 2 | 36.0000 | | 5 | 46.0000 | +---------+---------+ 3 rows in set (0.02 sec)
(4) 显示以L开头的名字的同学的信息;
MariaDB [hellodb]> select * from students where name like ‘L%‘; # LIKE子句匹配 +-------+-------------+-----+--------+---------+-----------+ | StuID | Name | Age | Gender | ClassID | TeacherID | +-------+-------------+-----+--------+---------+-----------+ | 8 | Lin Daiyu | 17 | F | 7 | NULL | | 14 | Lu Wushuang | 17 | F | 3 | NULL | | 17 | Lin Chong | 25 | M | 4 | NULL | +-------+-------------+-----+--------+---------+-----------+ 3 rows in set (0.01 sec) MariaDB [hellodb]> select * from students where name RLIKE ‘^L.*$‘; # 正则表达式匹配 +-------+-------------+-----+--------+---------+-----------+ | StuID | Name | Age | Gender | ClassID | TeacherID | +-------+-------------+-----+--------+---------+-----------+ | 8 | Lin Daiyu | 17 | F | 7 | NULL | | 14 | Lu Wushuang | 17 | F | 3 | NULL | | 17 | Lin Chong | 25 | M | 4 | NULL | +-------+-------------+-----+--------+---------+-----------+ 3 rows in set (0.00 sec)
(5) 显示TeacherID非空的同学的相关信息;
MariaDB [hellodb]> select Name,TeacherID from students where TeacherID is not null; +-------------+-----------+ | Name | TeacherID | +-------------+-----------+ | Shi Zhongyu | 3 | | Shi Potian | 7 | | Xie Yanke | 16 | | Ding Dian | 4 | | Yu Yutong | 1 | +-------------+-----------+
(6) 以年龄排序后,显示年龄最大的前10位同学的信息;
MariaDB [hellodb]> select * from students order by Age desc; # 降序排列 +-------+---------------+-----+--------+---------+-----------+ | StuID | Name | Age | Gender | ClassID | TeacherID | +-------+---------------+-----+--------+---------+-----------+ | 25 | Sun Dasheng | 100 | M | NULL | NULL | | 3 | Xie Yanke | 53 | M | 2 | 16 | | 6 | Shi Qing | 46 | M | 5 | NULL | | 13 | Tian Boguang | 33 | M | 2 | NULL | | 4 | Ding Dian | 32 | M | 4 | 4 | | 24 | Xu Xian | 27 | M | NULL | NULL | | 5 | Yu Yutong | 26 | M | 3 | 1 | | 17 | Lin Chong | 25 | M | 4 | NULL | | 23 | Ma Chao | 23 | M | 4 | NULL | | 18 | Hua Rong | 23 | M | 7 | NULL | | 11 | Yuan Chengzhi | 23 | M | 6 | NULL | | 21 | Huang Yueying | 22 | F | 6 | NULL | | 1 | Shi Zhongyu | 22 | M | 2 | 3 | | 2 | Shi Potian | 22 | M | 1 | 7 | | 16 | Xu Zhu | 21 | M | 1 | NULL | | 22 | Xiao Qiao | 20 | F | 1 | NULL | | 9 | Ren Yingying | 20 | F | 6 | NULL | | 15 | Duan Yu | 19 | M | 4 | NULL | | 7 | Xi Ren | 19 | F | 3 | NULL | | 20 | Diao Chan | 19 | F | 7 | NULL | | 10 | Yue Lingshan | 19 | F | 3 | NULL | | 12 | Wen Qingqing | 19 | F | 1 | NULL | | 19 | Xue Baochai | 18 | F | 6 | NULL | | 8 | Lin Daiyu | 17 | F | 7 | NULL | | 14 | Lu Wushuang | 17 | F | 3 | NULL | +-------+---------------+-----+--------+---------+-----------+ 25 rows in set (0.00 sec) MariaDB [hellodb]> select * from students order by Age desc limit 10; # 增加limit显示数量 +-------+--------------+-----+--------+---------+-----------+ | StuID | Name | Age | Gender | ClassID | TeacherID | +-------+--------------+-----+--------+---------+-----------+ | 25 | Sun Dasheng | 100 | M | NULL | NULL | | 3 | Xie Yanke | 53 | M | 2 | 16 | | 6 | Shi Qing | 46 | M | 5 | NULL | | 13 | Tian Boguang | 33 | M | 2 | NULL | | 4 | Ding Dian | 32 | M | 4 | 4 | | 24 | Xu Xian | 27 | M | NULL | NULL | | 5 | Yu Yutong | 26 | M | 3 | 1 | | 17 | Lin Chong | 25 | M | 4 | NULL | | 23 | Ma Chao | 23 | M | 4 | NULL | | 18 | Hua Rong | 23 | M | 7 | NULL | +-------+--------------+-----+--------+---------+-----------+ 10 rows in set (0.00 sec)
(7) 查询年龄大于等于20岁,小于等于25岁的同学的信息;用三种方法;
MariaDB [hellodb]> select Name,Age from students where Age >= 20 and Age <= 25; +---------------+-----+ | Name | Age | +---------------+-----+ | Shi Zhongyu | 22 | | Shi Potian | 22 | | Ren Yingying | 20 | | Yuan Chengzhi | 23 | | Xu Zhu | 21 | | Lin Chong | 25 | | Hua Rong | 23 | | Huang Yueying | 22 | | Xiao Qiao | 20 | | Ma Chao | 23 | +---------------+-----+ 10 rows in set (0.00 sec) MariaDB [hellodb]> select Name,Age from students where Age between 20 and 25; +---------------+-----+ | Name | Age | +---------------+-----+ | Shi Zhongyu | 22 | | Shi Potian | 22 | | Ren Yingying | 20 | | Yuan Chengzhi | 23 | | Xu Zhu | 21 | | Lin Chong | 25 | | Hua Rong | 23 | | Huang Yueying | 22 | | Xiao Qiao | 20 | | Ma Chao | 23 | +---------------+-----+ 10 rows in set (0.00 sec) MariaDB [hellodb]> select Name,Age from students where Age in (20,21,22,23,2425); +---------------+-----+ | Name | Age | +---------------+-----+ | Shi Zhongyu | 22 | | Shi Potian | 22 | | Ren Yingying | 20 | | Yuan Chengzhi | 23 | | Xu Zhu | 21 | | Lin Chong | 25 | | Hua Rong | 23 | | Huang Yueying | 22 | | Xiao Qiao | 20 | | Ma Chao | 23 | +---------------+-----+ 10 rows in set (0.00 sec)
练习2
1、以ClassID分组,显示每班的同学的人数;
MariaDB [hellodb]> select ClassID,count(StuID) from students group by ClassID; +---------+--------------+ | ClassID | count(StuID) | +---------+--------------+ | NULL | 2 | | 1 | 4 | | 2 | 3 | | 3 | 4 | | 4 | 4 | | 5 | 1 | | 6 | 4 | | 7 | 3 | +---------+--------------+ 8 rows in set (0.01 sec)
2、以Gender分组,显示其年龄之和;
MariaDB [hellodb]> select Gender,sum(age) as sum_age from students group by Gender; +--------+---------+ | Gender | sum_age | +--------+---------+ | F | 190 | | M | 495 | +--------+---------+ 2 rows in set (0.01 sec)
3、以ClassID分组,显示其平均年龄大于25的班级;
MariaDB [hellodb]> select ClassID,avg(age) as avg_age from students group by ClassID having avg_age > 25; +---------+---------+ | ClassID | avg_age | +---------+---------+ | NULL | 63.5000 | | 2 | 36.0000 | | 5 | 46.0000 | +---------+---------+ 3 rows in set (0.01 sec)
4、以Gender分组,显示各组中年龄大于25的学员的年龄之和;
MariaDB [hellodb]> select Gender,sum(age) from students where age > 25 group by Gender; +--------+----------+ | Gender | sum(age) | +--------+----------+ | M | 317 | +--------+----------+ 1 row in set (0.02 sec)
SELECT:多表查询
★连接操作:
☉交叉连接:笛卡尔乘积;
☉内连接:
等值连接:让表之间的字段以等值的方式建立连接;
不等值连接:
自然连接
自连接
☉外连接:
左外连接: FROM tb1 LEFT JOIN tb2 ON tb1.col = tb2.col
右外连接: FROM tb1 RIGHT JOIN tb2 ON tb1.col = tb2.col
演示:
1.等值连接,挑选students表中的ClassID和classes表中的ClassID相等的同学的信息;
MariaDB [hellodb]> select * from classes; +---------+----------------+----------+ | ClassID | Class | NumOfStu | +---------+----------------+----------+ | 1 | Shaolin Pai | 10 | | 2 | Emei Pai | 7 | | 3 | QingCheng Pai | 11 | | 4 | Wudang Pai | 12 | | 5 | Riyue Shenjiao | 31 | | 6 | Lianshan Pai | 27 | | 7 | Ming Jiao | 27 | | 8 | Xiaoyao Pai | 15 | +---------+----------------+----------+ 8 rows in set (0.00 sec) MariaDB [hellodb]> select * from students,classes where students.ClassID = classes.ClassID; +-------+---------------+-----+--------+---------+-----------+---------+----------------+----------+ | StuID | Name | Age | Gender | ClassID | TeacherID | ClassID | Class | NumOfStu | +-------+---------------+-----+--------+---------+-----------+---------+----------------+----------+ | 1 | Shi Zhongyu | 22 | M | 2 | 3 | 2 | Emei Pai | 7 | | 2 | Shi Potian | 22 | M | 1 | 7 | 1 | Shaolin Pai | 10 | | 3 | Xie Yanke | 53 | M | 2 | 16 | 2 | Emei Pai | 7 | | 4 | Ding Dian | 32 | M | 4 | 4 | 4 | Wudang Pai | 12 | | 5 | Yu Yutong | 26 | M | 3 | 1 | 3 | QingCheng Pai | 11 | | 6 | Shi Qing | 46 | M | 5 | NULL | 5 | Riyue Shenjiao | 31 | | 7 | Xi Ren | 19 | F | 3 | NULL | 3 | QingCheng Pai | 11 | | 8 | Lin Daiyu | 17 | F | 7 | NULL | 7 | Ming Jiao | 27 | | 9 | Ren Yingying | 20 | F | 6 | NULL | 6 | Lianshan Pai | 27 | | 10 | Yue Lingshan | 19 | F | 3 | NULL | 3 | QingCheng Pai | 11 | | 11 | Yuan Chengzhi | 23 | M | 6 | NULL | 6 | Lianshan Pai | 27 | | 12 | Wen Qingqing | 19 | F | 1 | NULL | 1 | Shaolin Pai | 10 | | 13 | Tian Boguang | 33 | M | 2 | NULL | 2 | Emei Pai | 7 | | 14 | Lu Wushuang | 17 | F | 3 | NULL | 3 | QingCheng Pai | 11 | | 15 | Duan Yu | 19 | M | 4 | NULL | 4 | Wudang Pai | 12 | | 16 | Xu Zhu | 21 | M | 1 | NULL | 1 | Shaolin Pai | 10 | | 17 | Lin Chong | 25 | M | 4 | NULL | 4 | Wudang Pai | 12 | | 18 | Hua Rong | 23 | M | 7 | NULL | 7 | Ming Jiao | 27 | | 19 | Xue Baochai | 18 | F | 6 | NULL | 6 | Lianshan Pai | 27 | | 20 | Diao Chan | 19 | F | 7 | NULL | 7 | Ming Jiao | 27 | | 21 | Huang Yueying | 22 | F | 6 | NULL | 6 | Lianshan Pai | 27 | | 22 | Xiao Qiao | 20 | F | 1 | NULL | 1 | Shaolin Pai | 10 | | 23 | Ma Chao | 23 | M | 4 | NULL | 4 | Wudang Pai | 12 | +-------+---------------+-----+--------+---------+-----------+---------+----------------+----------+ 23 rows in set (0.00 sec) # 注意,如果两张表做等值连接时如果每个表中的字段唯一,则可以省略表名称,这里select name,Class也正确 MariaDB [hellodb]> select students.name,classes.Class from students,classes where students.ClassID = classes.ClassID; +---------------+----------------+ | name | Class | +---------------+----------------+ | Shi Zhongyu | Emei Pai | | Shi Potian | Shaolin Pai | | Xie Yanke | Emei Pai | | Ding Dian | Wudang Pai | | Yu Yutong | QingCheng Pai | | Shi Qing | Riyue Shenjiao | | Xi Ren | QingCheng Pai | | Lin Daiyu | Ming Jiao | | Ren Yingying | Lianshan Pai | | Yue Lingshan | QingCheng Pai | | Yuan Chengzhi | Lianshan Pai | | Wen Qingqing | Shaolin Pai | | Tian Boguang | Emei Pai | | Lu Wushuang | QingCheng Pai | | Duan Yu | Wudang Pai | | Xu Zhu | Shaolin Pai | | Lin Chong | Wudang Pai | | Hua Rong | Ming Jiao | | Xue Baochai | Lianshan Pai | | Diao Chan | Ming Jiao | | Huang Yueying | Lianshan Pai | | Xiao Qiao | Shaolin Pai | | Ma Chao | Wudang Pai | +---------------+----------------+ 23 rows in set (0.00 sec)
MariaDB [hellodb]> select * from students,teachers where students.TeacherID = teachers.TID; +-------+-------------+-----+--------+---------+-----------+-----+---------------+-----+--------+ | StuID | Name | Age | Gender | ClassID | TeacherID | TID | Name | Age | Gender | +-------+-------------+-----+--------+---------+-----------+-----+---------------+-----+--------+ | 5 | Yu Yutong | 26 | M | 3 | 1 | 1 | Song Jiang | 45 | M | | 1 | Shi Zhongyu | 22 | M | 2 | 3 | 3 | Miejue Shitai | 77 | F | | 4 | Ding Dian | 32 | M | 4 | 4 | 4 | Lin Chaoying | 93 | F | +-------+-------------+-----+--------+---------+-----------+-----+---------------+-----+--------+ 3 rows in set (0.01 sec) MariaDB [hellodb]> select * from teachers; +-----+---------------+-----+--------+ | TID | Name | Age | Gender | +-----+---------------+-----+--------+ | 1 | Song Jiang | 45 | M | | 2 | Zhang Sanfeng | 94 | M | | 3 | Miejue Shitai | 77 | F | | 4 | Lin Chaoying | 93 | F | +-----+---------------+-----+--------+ 4 rows in set (0.00 sec) MariaDB [hellodb]> select * from students where TeacherID is not null; +-------+-------------+-----+--------+---------+-----------+ | StuID | Name | Age | Gender | ClassID | TeacherID | +-------+-------------+-----+--------+---------+-----------+ | 1 | Shi Zhongyu | 22 | M | 2 | 3 | | 2 | Shi Potian | 22 | M | 1 | 7 | | 3 | Xie Yanke | 53 | M | 2 | 16 | | 4 | Ding Dian | 32 | M | 4 | 4 | | 5 | Yu Yutong | 26 | M | 3 | 1 | +-------+-------------+-----+--------+---------+-----------+ 5 rows in set (0.00 sec) # 注意,这里在students表中的外键TeacherID中是teachers表中的主键,但是students表中的外键TID # 中的数据在teachers表中的主键TID中并不存在,再innoDB存储引擎中是不被允许的(即外键约束),但在 # MyISAM中不支持
2.自连接
MariaDB [hellodb]> select * from students as s,students as t where s.TeacherID = t.StuID; +-------+-------------+-----+--------+---------+-----------+-------+-------------+-----+--------+---------+-----------+ | StuID | Name | Age | Gender | ClassID | TeacherID | StuID | Name | Age | Gender | ClassID | TeacherID | +-------+-------------+-----+--------+---------+-----------+-------+-------------+-----+--------+---------+-----------+ | 1 | Shi Zhongyu | 22 | M | 2 | 3 | 3 | Xie Yanke | 53 | M | 2 | 16 | | 2 | Shi Potian | 22 | M | 1 | 7 | 7 | Xi Ren | 19 | F | 3 | NULL | | 3 | Xie Yanke | 53 | M | 2 | 16 | 16 | Xu Zhu | 21 | M | 1 | NULL | | 4 | Ding Dian | 32 | M | 4 | 4 | 4 | Ding Dian | 32 | M | 4 | 4 | | 5 | Yu Yutong | 26 | M | 3 | 1 | 1 | Shi Zhongyu | 22 | M | 2 | 3 | +-------+-------------+-----+--------+---------+-----------+-------+-------------+-----+--------+---------+-----------+ 5 rows in set (0.00 sec) MariaDB [hellodb]> select s.name,t.name from students as s,students as t where s.TeacherID = t.StuID; +-------------+-------------+ | name | name | +-------------+-------------+ | Shi Zhongyu | Xie Yanke | | Shi Potian | Xi Ren | | Xie Yanke | Xu Zhu | | Ding Dian | Ding Dian | | Yu Yutong | Shi Zhongyu | +-------------+-------------+ 5 rows in set (0.00 sec)
本文出自 “逐梦小涛” 博客,请务必保留此出处http://1992tao.blog.51cto.com/11606804/1876721
原文地址:http://1992tao.blog.51cto.com/11606804/1876721