MySQL查询杂记

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

MySQL查询杂记的相关文章

MySQL 命令杂记

mysql> show processlist; 如果是root帐号,你能看到所有用户的当前连接.如果是其它普通帐号,只能看到自己占用的连接.show processlist;只列出前100条,如果想全列出请使用show full processlist; mysql> show status; mysql> show status like '%下面变量%'; Aborted_clients 由于客户没有正确关闭连接已经死掉,已经放弃的连接数量. Aborted_connects 尝试

MySQL 查询语句使用进阶

MySQL 查询语句使用进阶 =============================================================================== 概述: =============================================================================== 练习: 练习1  首先导入hellodb.sql的脚本文件,查询其数据库和表如下: [[email protected] ~]# mysql 

mysql查询练习

mysql> #查询每个栏目最贵的商品 mysql> select goods_id,shop_price,cat_id from (select goods_id,shop_price,cat_id from goods order by shop_price desc) as temp group by cat_id; +----------+------------+--------+ | goods_id | shop_price | cat_id | +----------+----

Mysql查询缓存碎片、缓存命中率及Nagios监控

Mysql 的优化方案,在互联网上可以查找到非常多资料,今天对Mysql缓存碎片和命中率作了详细了解,个人作了简单整理. 一.Mysql查询缓存碎片和缓存命中率. mysql> SHOW STATUS LIKE 'qcache%'; +-------------------------+-----------+ | Variable_name | Value | +-------------------------+-----------+ | Qcache_free_blocks | 5 |

MySQL查询in操作 查询结果按in集合顺序显示_Mysql_脚本之家

body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI",Tahoma,Helvetica,Sans-Serif,"Microsoft YaHei", Georgia,Helvetica,Arial,sans-serif,宋体, PMingLiU,serif; font-size: 10.5pt; line-height: 1.5;

MySQL查询计划输出列的含义

"一:MySQL查询计划输出列的含义:1.id:每个被独立执行的操作的标识,表示对象被操作的顺序:id值越大,先被执行:如果相同,执行顺序从上到下.2.select_type:查询中每个select子句的类型.3.table:名字,被操作的对象的名称,通常是表名,但有其他格式.4.partitions:匹配的分区信息(对于非分区表值为NULL).5.type:连接操作的类型.6.possible_keys:备选的索引(列出可能被使用到的索引).7.key:经优化器选定的索引:常使用ANALYZE

mysql查询缓存打开、设置、参数查询、性能变量意思

http://blog.sina.com.cn/s/blog_75ad10100101by7j.html http://www.cnblogs.com/zemliu/archive/2013/08/03/3234372.html 第一: query_cache_type 使用查询缓存的方式 一般,我们会把 query_cache_type 设置为 ON,默认情况下应该是ON mysql> select @@query_cache_type;+--------------------+| @@qu

MySQL查询本周、上周、本月、上个月份数据的sql代码(转)

感谢:http://www.jb51.net/article/32277.htm ------------------------------------------------------------------------------ MySQL查询的方式很多,下面为您介绍的MySQL查询实现的是查询本周.上周.本月.上个月份的数据,如果您对MySQL查询方面感兴趣的话,不妨一看 查询当前这周的数据 SELECT name,submittime FROM enterprise WHERE Y

Mysql 查询注意和执行shell命令

Mysql 查询注意 1. 在mysql查询的时候需要注意在表的前面加上数据库的前缀,不然就是默认是当前的数据库(当多个库查询的时候,可能会出现重复的查相同的表多次) 2. \! ls –al ,mysql可以执行shell 外部的命令(注意\!和ls之间的空格) 版权声明:本文为博主原创文章,未经博主允许不得转载.