select语句的逻辑执行顺序,你知道吗?

回顾一下上一篇博客说到的问题:

mysql -uroot -ptest

我们不能赤裸裸的将账户和密码就这样写在你的脚本里,这并不是一个好做法。所有能够访问你脚本的人都会知道数据库的用户账户和密码。要解决这个问题,我这里给大家提供一种做法:

第一种:编辑数据库的配置文件 /etc/my.cnf

[root@localhost ~]# vim /etc/my.cnf
[mysqld]
......
skip_grant_tables
......

在[mysqld]中添加一行:skip_grant_tables,跳过启动数据库时的安全认证,这样的话,我们在脚本中就可以不必输入密码而进行登录了。当然,为了安全,脚本执行完之后,你最好还是把这一行注释掉。每一次修改配置文件都需要重启服务。

好了,进入今天的正题!

select语句的逻辑执行顺序,你知道吗?

引言:

这个知识点不是很深的技术问题,但是他会让你对sql的编写、排忧及优化上会有很大的帮助。

但是你可以问一问你周围的人,有几个注意到这个问题了?又有几个能将这个问题讲的明明白白的?

正文:

select语句的逻辑执行顺序

下面是SELECT语句的逻辑执行顺序:

  1. from
  2. join
  3. on
  4. where
  5. group by
  6. avg,sum,count等各种函数
  7. having
  8. select
  9. distinct
  10. order by(asc(升序),desc(降序))
  11. LIMIT

示例:

示例一、查出学生id前7名的同学信息。

我这里有一张学生表,信息如下

MariaDB [hellodb]> select * from students;
+-------+----------------+-----+--------+---------+-----------+
| StuID | Name           | Age | Gender | ClassID | TeacherID |
+-------+----------------+-----+--------+---------+-----------+
|     1 | Hou Yi         |  22 | M      |       2 |         3 |
|     2 | Ya Se          |  22 | M      |       1 |         7 |
|     3 | An Qila        |  53 | F      |       2 |        16 |
|     4 | Da Ji          |  32 | F      |       4 |         4 |
|     5 | Sun Shangxiang |  26 | F      |       3 |         1 |
|     6 | Huang Zhong    |  46 | M      |       5 |      NULL |
|     7 | Liu Bei        |  19 | M      |       3 |      NULL |
|     8 | Guan Yu        |  17 | M      |       7 |      NULL |
|     9 | Zhang Fei      |  20 | M      |       6 |      NULL |

然后我们这样打

MariaDB [hellodb]> select stuid,name from students order by stuid where stuid <= 7;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that 

corresponds to your MariaDB server version for the right syntax to use near 'where stuid <= 7' at line 1

是不是很熟悉?很明显,这样打是错误的,因为where语句要在 order by 之前才能生效。正确的写法应该的这样子的:

MariaDB [hellodb]> select stuid,name from students where stuid <= 7 order by stuid;
+-------+----------------+
| stuid | name           |
+-------+----------------+
|     1 | Hou Yi         |
|     2 | Ya Se          |
|     3 | An Qila        |
|     4 | Da Ji          |
|     5 | Sun Shangxiang |
|     6 | Huang Zhong    |
|     7 | Liu Bei        |
+-------+----------------+

执行顺序如下:

1、from students;                   先查询students表。

2、where stuid <=7;                  在前一步的基础上列出满足where条件的行。

3、在第二步的基础上,按照stuid列排序。----------默认是降序排列,升序排列需要在by 后面加上 desc。

4、执行select stuid,name;

注:我们的每执行的一步,都是建立在上一步的基础之上。

示例二、以ClassID分组,显示每班的同学的人数

这里还用我们的students表。

MariaDB [hellodb]> select * from students;
+-------+----------------+-----+--------+---------+-----------+
| StuID | Name           | Age | Gender | ClassID | TeacherID |
+-------+----------------+-----+--------+---------+-----------+
|     1 | Hou Yi         |  22 | M      |       2 |         3 |
|     2 | Ya Se          |  22 | M      |       1 |         7 |
|     3 | An Qila        |  53 | F      |       2 |        16 |
|     4 | Da Ji          |  32 | F      |       4 |         4 |
|     5 | Sun Shangxiang |  26 | F      |       3 |         1 |
|     6 | Huang Zhong    |  46 | M      |       5 |      NULL |
|     7 | Liu Bei        |  19 | M      |       3 |      NULL |
|     8 | Guan Yu        |  17 | M      |       7 |      NULL |
|     9 | Zhang Fei      |  20 | M      |       6 |      NULL |
MariaDB [hellodb]>  select classid,count(classid) from students group by classid;
+---------+----------------+
| classid | count(classid) |
+---------+----------------+
|    NULL |              0 |
|       1 |              4 |
|       2 |              3 |
|       3 |              4 |
|       4 |              4 |
|       5 |              1 |
|       6 |              4 |
|       7 |              3 |
+---------+----------------+

执行顺序如下:

1、from students; 先查询students表

2、group by classid;以classid为分组依据,执行group by语句。

3、count函数计算出现相同classid字段的次数。

4、显示countid的值,并列出相同countid出现的次数。

这样,我们就达到了计算各班同学人数的目的。

猜你喜欢

转载自blog.csdn.net/qq_34208467/article/details/82817476
今日推荐