select语句的逻辑执行顺序,你知道吗?
回顧一下上一篇博客說(shuō)到的問(wèn)題:
mysql -uroot -ptest
我們不能赤裸裸的將賬戶和密碼就這樣寫在你的腳本里,這并不是一個(gè)好做法。所有能夠訪問(wèn)你腳本的人都會(huì)知道數(shù)據(jù)庫(kù)的用戶賬戶和密碼。要解決這個(gè)問(wèn)題,我這里給大家提供一種做法:
第一種:編輯數(shù)據(jù)庫(kù)的配置文件 /etc/my.cnf
[root@localhost ~]# vim /etc/my.cnf [mysqld] ...... skip_grant_tables ......在[mysqld]中添加一行:skip_grant_tables,跳過(guò)啟動(dòng)數(shù)據(jù)庫(kù)時(shí)的安全認(rèn)證,這樣的話,我們?cè)谀_本中就可以不必輸入密碼而進(jìn)行登錄了。當(dāng)然,為了安全,腳本執(zhí)行完之后,你最好還是把這一行注釋掉。每一次修改配置文件都需要重啟服務(wù)。
好了,進(jìn)入今天的正題!
select語(yǔ)句的邏輯執(zhí)行順序,你知道嗎?
引言:
這個(gè)知識(shí)點(diǎn)不是很深的技術(shù)問(wèn)題,但是他會(huì)讓你對(duì)sql的編寫、排憂及優(yōu)化上會(huì)有很大的幫助。
但是你可以問(wèn)一問(wèn)你周圍的人,有幾個(gè)注意到這個(gè)問(wèn)題了?又有幾個(gè)能將這個(gè)問(wèn)題講的明明白白的?
正文:
select語(yǔ)句的邏輯執(zhí)行順序
下面是SELECT語(yǔ)句的邏輯執(zhí)行順序:
示例:
示例一、查出學(xué)生id前7名的同學(xué)信息。
我這里有一張學(xué)生表,信息如下
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是不是很熟悉?很明顯,這樣打是錯(cuò)誤的,因?yàn)閣here語(yǔ)句要在 order by 之前才能生效。正確的寫法應(yīng)該的這樣子的:
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 | +-------+----------------+執(zhí)行順序如下:
1、from students; ? ? ? ? ? ? ? ? ? 先查詢students表。
2、where stuid <=7; ? ? ? ? ? ? ? ?? 在前一步的基礎(chǔ)上列出滿足where條件的行。
3、在第二步的基礎(chǔ)上,按照stuid列排序。----------默認(rèn)是降序排列,升序排列需要在by 后面加上 desc。
4、執(zhí)行select stuid,name;
注:我們的每執(zhí)行的一步,都是建立在上一步的基礎(chǔ)之上。
示例二、以ClassID分組,顯示每班的同學(xué)的人數(shù)
這里還用我們的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 | +---------+----------------+執(zhí)行順序如下:
1、from students; 先查詢students表
2、group by classid;以classid為分組依據(jù),執(zhí)行g(shù)roup by語(yǔ)句。
3、count函數(shù)計(jì)算出現(xiàn)相同classid字段的次數(shù)。
4、顯示countid的值,并列出相同countid出現(xiàn)的次數(shù)。
這樣,我們就達(dá)到了計(jì)算各班同學(xué)人數(shù)的目的。
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的select语句的逻辑执行顺序,你知道吗?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Corel VideoStudio会声会
- 下一篇: WEB网络渗透