天天看点

MySQL/MariaDB DML操作之Select

前言

上文我们已经讲解了MySQL/MariaDB的基础知识和DDL相关操作,接下来我们来说一下MySQL/MariaDB的DML操作,因select查询较复杂也较重要,所以本文主要是对select查询的详解。

DML操作

DML之select

投影查询

1

2

3

4

<code>select</code> <code>col_name,[col_name1,...] from table_name;</code>

<code>select</code> <code>* from table_name ;</code><code>#显示全表</code>

<code>遍历整张数据表,但对系统资源消耗较大,再进行大数据量的查询时,禁止使用这类操作</code>

条件查询

5

6

7

8

9

10

11

12

13

14

15

16

<code>select</code> <code>col_name,[col_name1,...] from table_name where where_definition</code>

<code>#条件比较操作符</code>

<code>=    </code><code>#等值比较</code>

<code>&lt;=&gt;  </code><code>#等值比较,包括与NULL的安全比较</code>

<code>&lt;&gt;或!=  </code><code>#不等值比较</code>

<code>&lt;,&lt;=,&gt;,&gt;=  </code><code>#其它比较符</code>

<code>IN  </code><code>#指定范围内值的存在性测试</code>

<code>BETWEEN … AND …  </code><code>#在某取值范围内</code>

<code>IS NULL  </code><code>#是否为空值</code>

<code>IS NOT NULL  </code><code>#是否为非空</code>

<code>LIKE  </code><code>#可使用通配符:%, _</code>

<code>RLIKE或REGEXP  </code><code>#可使用正则表达式的模式</code>

<code>#逻辑操作符</code>

<code>AND </code>

<code>OR</code>

<code>NOT</code>

 聚合查询

<code>AVG():平均值 </code>

<code>SUM():总和</code>

<code>MAX():最大值</code>

<code>MIN():最小值</code>

<code>COUNT():记录总数</code>

子句修饰符

<code>GROUP BY </code><code>#对符合条件的结果进行分组</code>

<code>HAVING: 对聚合查询的结果做过滤</code>

<code>ORDER BY col1[,col2] {ASC|DESC} </code><code>#排序</code>

<code>LIMIT [Offset,]count </code><code>#限制输出行数</code>

select执行流程

<code>FROM --&gt; WHERE --&gt; GROUP BY --&gt; HAVING --&gt; ORDER BY --&gt; SELECT --&gt; LIMIT</code>

连接查询

事先将两张或多张表执行相应的join操作,而后根据join结果做查询

<code>CROSS JOIN:笛卡尔积,交叉连接</code>

<code>select</code> <code>* from tab1,tab2; </code>

<code>INNER JOIN:内连接</code>

<code>#等值连接</code>

<code>select</code> <code>* from tab1 inner </code><code>join</code> <code>tab2 on tab1.col_name = tab2.col_name</code>

<code>OUTER JOIN:外连接</code>

<code>LEFT OUTER JOIN:左外连接</code>

<code>select</code> <code>s.name as student,t.name as teacher from students as s lift </code><code>join</code> <code>teachers as t </code>

<code>on s.TeacherID = t.TID;  </code>

<code>RIGHT OUTER JOIN:右外连接    </code>

<code>select</code> <code>s.name as student,t.name as teacher from students as s right </code><code>join</code> <code>teachers as t</code>

<code>on s.TeacherID = t.TID;    </code>

<code>NATURAL JOIN:自然连接,等值连接</code>

<code>select</code> <code>tab1.col_name,tab2.col_name from tab1,tab2 where tab1.col_name = tab2.col_name;</code>

子查询

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

用于where子句的子查询

<code>①用于比较表达式中的子查询</code>

<code>要求子查询只能返回单个结果              </code>

<code>select</code> <code>Name,Age from students where Age &gt; (</code><code>select</code> <code>AVG(Age) from students);</code>

<code>②用于</code><code>in</code><code>中的子查询</code>

<code>判断是否存在于指定的列表中</code>

<code>select</code> <code>Name from students where StuID </code><code>in</code> <code>(</code><code>select</code> <code>TID from teschers);</code>

<code>③用于exists中子查询</code>

用于from中的子查询

<code>select</code> <code>alias</code><code>.col,... from (</code><code>select</code> <code>statement) as </code><code>alias</code> <code>where clause</code>

<code>select</code> <code>s.Name from (</code><code>select</code> <code>* from students where Age &gt; 20) as s where s.Name like </code><code>'s%'</code><code>;</code>

注:MySQL/MariaDB在子查询优化方面并不成熟,所以尽量避免使用子查询

联合查询

将两外或多个返回值字段相同的查询的结果合并输出

<code>select</code> <code>statement union </code><code>select</code> <code>statement</code>

<code>select</code> <code>Name,Age from teachers where Age&gt;=40 union </code><code>select</code> <code>Name,Age from students where Age&gt;=40;</code>

select实战

<code>表结构</code>

<a href="http://s3.51cto.com/wyfs02/M00/6C/75/wKioL1VJ3PvQAmUpAAKjBbhoib8087.jpg" target="_blank"></a>

<a href="http://s3.51cto.com/wyfs02/M01/6C/79/wKiom1VJ26DRdh3NAAH7mRXXnIA739.jpg" target="_blank"></a>

<code>以ClassID分组,显示每班的同学的人数</code>

<a href="http://s3.51cto.com/wyfs02/M01/6C/79/wKiom1VJ29vRLcI6AACq3EN5vzA400.jpg" target="_blank"></a>

<code>以Gender分组,显示其年龄之和</code>

<a href="http://s3.51cto.com/wyfs02/M00/6C/79/wKiom1VJ3IvxA-sKAAB6Gyk75Go008.jpg" target="_blank"></a>

<code>以ClassID分组,显示其平均年龄大于25的班级</code>

<a href="http://s3.51cto.com/wyfs02/M02/6C/75/wKioL1VJ35GhhSPVAACbXt1hYIs496.jpg" target="_blank"></a>

<code>以Gender分组,显示各组中年龄大于25的学员的年龄之和</code>

<a href="http://s3.51cto.com/wyfs02/M01/6C/79/wKiom1VJ3zLDcN4pAAByhZBzhJE193.jpg" target="_blank"></a>

<code>显示前5位同学的姓名、课程及成绩</code>

<a href="http://s3.51cto.com/wyfs02/M02/6C/76/wKioL1VJ40yxvGm1AAHMozrkQQU793.jpg" target="_blank"></a>

<code>显示其成绩高于80的同学的名称及课程</code>

<a href="http://s3.51cto.com/wyfs02/M02/6C/76/wKioL1VJ4_bgzH7kAAGQ7rHbyT8098.jpg" target="_blank"></a>

<code>求前8位同学每位同学自己两门课的平均成绩,并按降序排列</code>

<a href="http://s3.51cto.com/wyfs02/M02/6C/76/wKioL1VJ5puQXyfBAAFGckvbgUs609.jpg" target="_blank"></a>

<code>显示每门课程课程名称及学习了这门课的同学的个数</code>

<a href="http://s3.51cto.com/wyfs02/M00/6C/7A/wKiom1VJ6MOCvZFrAAEJAPED65E191.jpg" target="_blank"></a>

<code>显示其年龄大于平均年龄的同学的名字</code>

<a href="http://s3.51cto.com/wyfs02/M00/6C/7B/wKiom1VJ6cmgrkBJAACWDPkAnNo217.jpg" target="_blank"></a>

<code>统计各班级中年龄大于全校同学平均年龄的人数</code>

<a href="http://s3.51cto.com/wyfs02/M00/6C/76/wKioL1VJ7U6AEN4MAACyOqVPhD8468.jpg" target="_blank"></a>

The end

好了,select查询就先说到这里了,看起来select就这么点东西,其实要真正用好并不容易,所以小伙伴们多加练习吧,后续文章还会继续讲解MySQL/MariaDB系列的知识,有兴趣的可继续关注哦。以上仅为个人学习整理,如有错漏,大神勿喷~~~

本文转自 北城书生  51CTO博客,原文链接:http://blog.51cto.com/scholar/1643563