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語句的邏輯執行順序:
示例:
示例一、查出學生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出現的次數。
這樣,我們就達到了計算各班同學人數的目的。
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
總結
以上是生活随笔為你收集整理的select语句的逻辑执行顺序,你知道吗?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Corel VideoStudio会声会
- 下一篇: WEB网络渗透