MySQL查询
杂项
1、显示当前系统时间
MariaDB [hellodb]> SELECT NOW();
+---------------------+
| NOW() |
+---------------------+
| 2017-01-20 05:10:32 |
+---------------------+
2、去除重复的内容查询
只保留一样数据:
MariaDB [hellodb]> SELECT DISTINCT GENDER FROM students;
+--------+
| GENDER |
+--------+
| M |
| F |
+--------+
2.1
MariaDB [hellodb]> SHOW GLOBAL VARIABLES LIKE ‘query%‘;
+------------------------------+---------+
| Variable_name | Value |
+------------------------------+---------+
| query_alloc_block_size | 8192 |
| query_cache_limit | 1048576 |
| query_cache_min_res_unit | 4096 |
| query_cache_size | 0 |
| query_cache_strip_comments | OFF |
| query_cache_type | ON |
| query_cache_wlock_invalidate | OFF |
| query_prealloc_size | 8192 |
+------------------------------+---------+
2.2 查询缓存命中次数:
MariaDB [hellodb]> SHOW GLOBAL STATUS LIKE ‘Qcache%‘;
+-------------------------+-------+
| Variable_name | Value |
+-------------------------+-------+
| Qcache_free_blocks | 0 |
| Qcache_free_memory | 0 |
| Qcache_hits | 0 |
| Qcache_inserts | 0 |
| Qcache_lowmem_prunes | 0 |
| Qcache_not_cached | 0 |
| Qcache_queries_in_cache | 0 |
| Qcache_total_blocks | 0 |
2.3 查询查询总次数
MariaDB [hellodb]> SHOW GLOBAL STATUS LIKE ‘Com_se%‘;
+----------------+-------+
| Variable_name | Value |
+----------------+-------+
| Com_select | 57 |
| Com_set_option | 41 |
+----------------+-------+
3、查询字段别名
MariaDB [hellodb]> SELECT Name AS StuName FROM students;
+---------------+
| StuName |
+---------------+
| Diao Chan |
| Ding Dian |
| Duan Yu |
| Hua Rong |
| Huang Yueying |
……
4、表达式算术运算
4.1 查找年龄大于50的同学:
MariaDB [hellodb]> SELECT Name,Age FROM students WHERE Age > 50;
+--------------+-----+
| Name | Age |
+--------------+-----+
| Xie Yanke | 53 |
| Yinjiao King | 98 |
| Sun Dasheng | 100 |
| Jinjiao King | 100 |
+--------------+-----+
4.2 查找年龄加30后还大于50的同学:
MariaDB [hellodb]> SELECT Name,Age FROM students WHERE Age+30 > 50;
4.3 查询岁数不等于100的同学:
MariaDB [hellodb]> SELECT Name,Age FROM students WHERE Age != 100;
或
MariaDB [hellodb]> SELECT Name,Age FROM students WHERE Age <> 100;
4.4 查询20-40岁的同学:
MariaDB [hellodb]> SELECT Name,Age FROM students WHERE Age BETWEEN 20 AND 40;
4.5 IN,显示18和100岁的同学:
MariaDB [hellodb]> SELECT Name,Age FROM students WHERE Age IN (18,100);
+--------------+-----+
| Name | Age |
+--------------+-----+
| Xue Baochai | 18 |
| Sun Dasheng | 100 |
| Jinjiao King | 100 |
4.6 查询学生ID为空的学生:
MariaDB [hellodb]> SELECT Name,ClassID FROM students WHERE ClassID IS NULL;
+--------------+---------+
| Name | ClassID |
+--------------+---------+
| Xu Xian | NULL |
| Sun Dasheng | NULL |
| Jinjiao King | NULL |
| Yinjiao King | NULL |
+--------------+---------+
4.7 查找学生ID非空的学生:
MariaDB [hellodb]> SELECT Name,ClassID FROM students WHERE ClassID IS NOT NULL;
4.8 LIKE:
MariaDB [hellodb]> SELECT Name,Age FROM students WHERE Name LIKE "%Da";
Empty set (0.00 sec)
MariaDB [hellodb]> SELECT Name,Age FROM students WHERE Name LIKE "%eng";
+-------------+-----+
| Name | Age |
+-------------+-----+
| Sun Dasheng | 100 |
5、逻辑操作符
5.1 对全班同学性别分组,但是并不是显示所有同学:
MariaDB [hellodb]> SELECT * FROM students GROUP BY Gender;
+-------+-------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+-------------+-----+--------+---------+-----------+
| 7 | Xi Ren | 19 | F | 3 | NULL |
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
5.1.1 分组聚合,查看男女性别的平均年龄:
MariaDB [hellodb]> SELECT avg(Age),Gender FROM students GROUP BY Gender;
+----------+--------+
| avg(Age) | Gender |
+----------+--------+
| 19.0000 | F |
| 40.7647 | M |
+----------+--------+
5.1.2 查看平均年龄大于20的性别,HAVING针对GROUP BY做过滤
MariaDB [hellodb]> SELECT avg(Age) AS AAge,Gender FROM students GROUP BY Gender HAVING AAge>20;
+---------+--------+
| AAge | Gender |
+---------+--------+
| 40.7647 | M |
+---------+--------+
5.2 查看每个班级有多少人:
MariaDB [hellodb]> SELECT count(StuID) AS NOS,ClassID FROM students GROUP BY ClassID;
+-----+---------+
| NOS | ClassID |
+-----+---------+
| 4 | NULL |
| 4 | 1 |
| 3 | 2 |
| 4 | 3 |
| 4 | 4 |
| 1 | 5 |
| 4 | 6 |
| 3 | 7 |
+-----+---------+
5.3 查看班级总人数大于2的班级:
MariaDB [hellodb]> SELECT count(Stuid) AS NOS,ClassID FROM students GROUP BY ClassID HAVING NOS>2;
+-----+---------+
| NOS | ClassID |
+-----+---------+
| 4 | NULL |
| 4 | 1 |
| 3 | 2 |
| 4 | 3 |
| 4 | 4 |
| 4 | 6 |
| 3 | 7 |
6、升降序
6.1 默认升序:
MariaDB [hellodb]> SELECT Name,Age FROM students ORDER BY Age;
+---------------+-----+
| Name | Age |
+---------------+-----+
| Lu Wushuang | 17 |
| Lin Daiyu | 17 |
| Xue Baochai | 18 |
| Wen Qingqing | 19 |
| Duan Yu | 19 |
| Yue Lingshan | 19 |
| Xi Ren | 19 |
| Diao Chan | 19 |
| Ren Yingying | 20 |
| Xiao Qiao | 20 |
| Xu Zhu | 21 |
| Huang Yueying | 22 |
6.2 降序:
MariaDB [hellodb]> SELECT Name,Age FROM students ORDER BY Age DESC;
+---------------+-----+
| Name | Age |
+---------------+-----+
| Jinjiao King | 100 |
| Sun Dasheng | 100 |
| Yinjiao King | 98 |
| Xie Yanke | 53 |
| Shi Qing | 46 |
| Tian Boguang | 33 |
| Ding Dian | 32 |
7、LIMIT
7.1 只显示前5行:
MariaDB [hellodb]> SELECT Name,Age FROM students ORDER BY Age LIMIT 5;
+--------------+-----+
| Name | Age |
+--------------+-----+
| Lu Wushuang | 17 |
| Lin Daiyu | 17 |
| Xue Baochai | 18 |
| Wen Qingqing | 19 |
| Duan Yu | 19 |
+--------------+-----+
7.2 取第11-20个,第一个参数是偏移跳过去多少个:
MariaDB [hellodb]> SELECT NAME,Age FROM students ORDER BY Age DESC LIMIT 10,10;
多表查询
1、等值连接
1.1 查找两种表teacherID一样的行
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 |
+-------+-------------+-----+--------+---------+-----------+-----+---------------+-----+--------+
1.1 将金角和银角的teacherID修改为1,2
MariaDB [hellodb]> UPDATE students SET TeacherID=1 WHERE StuID=26;
MariaDB [hellodb]> UPDATE students SET TeacherID=2 WHERE StuID=27;
MariaDB [hellodb]> SELECT * FROM students WHERE Name LIKE "%King";
+-------+--------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+--------------+-----+--------+---------+-----------+
| 26 | Jinjiao King | 100 | M | NULL | 1 |
| 27 | Yinjiao King | 98 | M | NULL | 2 |
+-------+--------------+-----+--------+---------+-----------+
1.2
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 |
| 26 | Jinjiao King | 100 | M | NULL | 1 | 1 | Song Jiang | 45 | M |
| 27 | Yinjiao King | 98 | M | NULL | 2 | 2 | Zhang Sanfeng | 94 | 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 |
+-------+--------------+-----+--------+---------+-----------+-----+---------------+-----+--------+
1.3 查看每个学生的老师是谁:
MariaDB [hellodb]> SELECT s.Name AS StuName,t.Name AS TeaName FROM students AS s,teachers AS t WHERE s.TeacherID=t.TID;
+--------------+---------------+
| StuName | TeaName |
+--------------+---------------+
| Yu Yutong | Song Jiang |
| Jinjiao King | Song Jiang |
| Yinjiao King | Zhang Sanfeng |
| Shi Zhongyu | Miejue Shitai |
| Ding Dian | Lin Chaoying |
+--------------+---------------+
2、索引
2.1 查看多表查询能否使用索引:
MariaDB [hellodb]> EXPLAIN SELECT s.Name AS StuName,t.Name AS TeaName FROM students AS s,teachers AS t WHERE s.TeacherID=t.TID\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: s
type: ALL #全表扫描
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 27
Extra:
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: t
type: ALL
possible_keys: PRIMARY #只用了主键扫描
key: NULL
key_len: NULL
ref: NULL
rows: 4
Extra: Using where; Using join buffer (flat, BNL join)
2 rows in set (0.00 sec)
2.2 查看班级表:
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 |
+---------+----------------+----------+
2.3 查看每个同学属于哪个班级
MariaDB [hellodb]> SELECT s.Name,c.Class FROM students AS s,classes AS c WHERE s.ClassID=c.ClassID;
+---------------+----------------+
| Name | Class |
+---------------+----------------+
| Shi Zhongyu | Emei Pai |
| Shi Potian | Shaolin Pai |
| Xie Yanke | Emei Pai |
| Ding Dian | Wudang Pai |
……
3、左外连接,右外连接
3.1 显示所有同学的班级,没有班级的也显示
MariaDB [hellodb]> SELECT s.Name,c.Class FROM students AS s LEFT JOIN classes AS c ON s.ClassID=c.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 |
| Xu Xian | NULL |
| Sun Dasheng | NULL |
| Jinjiao King | NULL |
| Yinjiao King | NULL |
3.2 右连接,查看哪个班没人学
MariaDB [hellodb]> SELECT s.Name,c.Class FROM students AS s RIGHT JOIN classes AS c ON s.ClassID=c.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 |
| NULL | Xiaoyao Pai |
+---------------+----------------+
3.3 自连接,把一张表当成两张表来用
MariaDB [hellodb]> SELECT s.Name,t.Name AS Tname FROM students AS s,students AS t WHERE s.TeacherID=t.StuID;
+--------------+-------------+
| Name | Tname |
+--------------+-------------+
| Shi Zhongyu | Xie Yanke |
| Shi Potian | Xi Ren |
| Xie Yanke | Xu Zhu |
| Ding Dian | Ding Dian |
| Yu Yutong | Shi Zhongyu |
| Jinjiao King | Shi Zhongyu |
| Yinjiao King | Shi Potian |
+--------------+-------------+
4、子查询-WHERE
4.1 查询年龄大于平均年龄的同学:
求平均年龄:
MariaDB [hellodb]> SELECT avg(Age) FROM students;
MariaDB [hellodb]> SELECT Name,Age FROM students WHERE Age>(SELECT avg(Age) FROM students);
+--------------+-----+
| Name | Age |
+--------------+-----+
| Tian Boguang | 33 |
| Shi Qing | 46 |
| Xie Yanke | 53 |
| Yinjiao King | 98 |
| Sun Dasheng | 100 |
| Jinjiao King | 100 |
+--------------+-----+
4.2 查看其索引:
MariaDB [hellodb]> EXPLAIN SELECT Name,Age FROM students WHERE Age>(SELECT avg(Age) FROM students)\G
*************************** 1. row ***************************
id: 1
select_type: PRIMARY
table: students
type: range
possible_keys: Age
key: Age
key_len: 1
ref: NULL
rows: 7
Extra: Using where
*************************** 2. row ***************************
id: 2
select_type: SUBQUERY
table: students
type: index
possible_keys: NULL
key: Age
key_len: 1
ref: NULL
rows: 27
Extra: Using index
2 rows in set (0.00 sec)
5、子查询-IN
5.1 查询同学年龄跟老师年龄一样的
MariaDB [hellodb]> SELECT Name,Age FROM students WHERE Age IN (SELECT Age FROM teachers);
6、子语句-FROM
6.1 查询平均年龄大于25的班级
求每个班级平均年龄:
MariaDB [hellodb]> SELECT avg(Age),ClassID FROM students WHERE ClassID IS NOT NULL GROUP BY ClassID;
+----------+---------+
| avg(Age) | ClassID |
+----------+---------+
| 81.2500 | NULL |
| 20.5000 | 1 |
| 36.0000 | 2 |
| 20.2500 | 3 |
| 24.7500 | 4 |
| 46.0000 | 5 |
| 20.7500 | 6 |
| 19.6667 | 7 |
+----------+---------+
MariaDB [hellodb]> 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;
+---------+---------+
| aage | ClassID |
+---------+---------+
| 36.0000 | 2 |
| 46.0000 | 5 |
7、联合查询
7.1 将两张表的查询结果连在一起,数值意志
MariaDB [hellodb]> SELECT Name,Age FROM students UNION SELECT Name,Age FROM teachers;
+---------------+-----+
| Name | Age |
+---------------+-----+
| Shi Zhongyu | 22 |
| Shi Potian | 22 |
| Xie Yanke | 53 |
| Ding Dian | 32 |
……
7.2 查看上班查询语句的索引,三个查询语句:
MariaDB [hellodb]> EXPLAIN SELECT Name,Age FROM students UNION SELECT Name,Age FROM teachers\G
*************************** 1. row ***************************
id: 1
select_type: PRIMARY
table: students
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 27
Extra:
*************************** 2. row ***************************
id: 2
select_type: UNION
table: teachers
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 4
Extra:
*************************** 3. row ***************************
id: NULL
select_type: UNION RESULT
table: <union1,2>
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: NULL
Extra:
3 rows in set (0.01 sec)
MariaDB [hellodb]>
7.3
原文地址:http://blog.51cto.com/zhongle21/2087363
时间: 2024-10-17 21:25:02