1. 程式人生 > >python3.5+pymysql學習資料庫(4)查詢語句學習

python3.5+pymysql學習資料庫(4)查詢語句學習

資料準備好以後先學習查詢操作。直接開始做題還是太難了,畢竟太多年沒有用過SQL,還是腳踏實地從基礎開始學習比較靠譜。

學習資料地址:https://www.w3cschool.cn/sql/zk9swfor.html


# 查詢學生資訊表全部資訊
sql = "SELECT * FROM edu.student "

查詢結果:

(1, 'zhaolei', '1990-01-01', 'M')
(2, 'Jimmy', '1990-1-19', 'M')
(3, 'Lucy', '1989-2-19', 'F')
(4, 'Kate', '1991-7-6', 'F')
(5, 'Seven', '1990-12-4', 'M')


#查詢學生資訊表中的學生編號和學生姓名兩列:
sql = "SELECT SID,Sname FROM edu.student "

查詢結果:

(1, 'zhaolei')
(2, 'Jimmy')
(3, 'Lucy')
(4, 'Kate')
(5, 'Seven')


#查詢SID等於01的學生資訊(查詢特定行)
sql = "SELECT * FROM edu.student WHERE SID = '01' "

查詢結果為:
(1, 'zhaolei', '1990-01-01', 'M')


#查詢成績表中的學生編號SID
sql = "SELECT SID FROM edu.sc "

查詢結果為:
(1,)
(1,)
(1,)
(2,)
(2,)
(2,)
(3,)
(3,)
(3,)
(4,)
(4,)
(4,)
(5,)
(5,)
(6,)
(6,)
(7,)
(7,)

#查詢結果去重複
sql = "SELECT DISTINCT SID FROM edu.sc "

查詢結果為:
(1,)
(2,)
(3,)
(4,)
(5,)
(6,)
(7,)

#查詢成績表資訊(學生編號去重複)
sql = "SELECT DISTINCT SID,CID, score FROM edu.sc "

查詢結果為:
(1, '01', '80')
(1, '02', '90')
(1, '03', '99')
(2, '01', '70')
(2, '02', '60')
(2, '03', '80')
(3, '01', '80')
(3, '02', '80')
(3, '03', '80')
(4, '01', '50')
(4, '02', '30')
(4, '03', '20')
(5, '01', '76')
(5, '02', '87')
(6, '01', '31')
(6, '03', '34')
(7, '02', '89')
(7, '03', '98')


#查詢成績表中學生數量
sql = "SELECT COUNT(SID) FROM edu.sc "

查詢結果為:
(18,)

#查詢成績表中學生數量(去重複)
sql = "SELECT COUNT(DISTINCT SID) FROM edu.sc "

查詢結果為:
(7,)


查詢中WHERE子句的使用

等於=

#查詢成績表中學生編號為04的所有行
sql = "SELECT * FROM edu.sc  WHERE SID = '04'"

查詢結果為:
(4, '01', '50')
(4, '02', '30')
(4, '03', '20')

不等於<>

#查詢成績表中學生編號不等於04的所有行
sql = "SELECT * FROM edu.sc  WHERE SID <> '04'"

查詢結果為:
(1, '01', '80')
(1, '02', '90')
(1, '03', '99')
(2, '01', '70')
(2, '02', '60')
(2, '03', '80')
(3, '01', '80')
(3, '02', '80')
(3, '03', '80')
(5, '01', '76')
(5, '02', '87')
(6, '01', '31')
(6, '03', '34')
(7, '02', '89')
(7, '03', '98')

>大於

#查詢成績表中學生編號大於04的所有行
sql = "SELECT * FROM edu.sc  WHERE SID > '04'"

查詢結果為:
(5, '01', '76')
(5, '02', '87')
(6, '01', '31')
(6, '03', '34')
(7, '02', '89')
(7, '03', '98')

小於<

#查詢成績表中學生編號小於04的所有行
sql = "SELECT * FROM edu.sc  WHERE SID < '04'"

查詢結果為:
(1, '01', '80')
(1, '02', '90')
(1, '03', '99')
(2, '01', '70')
(2, '02', '60')
(2, '03', '80')
(3, '01', '80')
(3, '02', '80')
(3, '03', '80')

小於等於<=

#查詢成績表中學生編號小於等於04的所有行
sql = "SELECT * FROM edu.sc  WHERE SID <= '04'"

查詢結果為:
(1, '01', '80')
(1, '02', '90')
(1, '03', '99')
(2, '01', '70')
(2, '02', '60')
(2, '03', '80')
(3, '01', '80')
(3, '02', '80')
(3, '03', '80')
(4, '01', '50')
(4, '02', '30')
(4, '03', '20')


大於等於>=

#查詢成績表中學生編號大於等於04的所有行
sql = "SELECT * FROM edu.sc  WHERE SID >= '04'"

查詢結果為:
(4, '01', '50')
(4, '02', '30')
(4, '03', '20')
(5, '01', '76')
(5, '02', '87')
(6, '01', '31')
(6, '03', '34')
(7, '02', '89')
(7, '03', '98')

BETWEEN在某個範圍內

 

#查詢成績表中學生編號在1和3之間的行
sql = "SELECT * FROM edu.sc WHERE SID BETWEEN 1 AND 3"

查詢結果為:
(1, '01', '80')
(1, '02', '90')
(1, '03', '99')
(2, '01', '70')
(2, '02', '60')
(2, '03', '80')
(3, '01', '80')
(3, '02', '80')
(3, '03', '80')

LIKE搜尋某種模式

#查詢學生資訊表中,名字以y結尾的行
sql = "SELECT * FROM edu.student WHERE Sname Like '%y'"

查詢結果為:
(2, 'Jimmy', '1990-1-19', 'M')
(3, 'Lucy', '1989-2-19', 'F')

#查詢學生資訊表中,名字包含im的行
sql = "SELECT * FROM edu.student WHERE Sname Like '%im%'"

查詢結果為:
(2, 'Jimmy', '1990-1-19', 'M')

IN為列指定多個可能的值

#查詢學生資訊表中,學生編號為1或者2的行
sql = "SELECT * FROM edu.student WHERE SID IN(1,2)"

查詢結果為:
(1, 'zhaolei', '1990-01-01', 'M')
(2, 'Jimmy', '1990-1-19', 'M')


AND,OR和NOT

#查詢學生資訊表中,學生編號為1並且課程編號為3的行
sql = "SELECT * FROM edu.sc WHERE SID=1 AND CID=3"

查詢結果為:
(1, '03', '99')

#查詢學生資訊表中,學生編號為1或者課程編號為3的行
sql = "SELECT * FROM edu.sc WHERE SID=1 OR CID=3"

查詢結果為:
(1, '01', '80')
(1, '02', '90')
(1, '03', '99')
(2, '03', '80')
(3, '03', '80')
(4, '03', '20')
(6, '03', '34')
(7, '03', '98')

#查詢學生資訊表中,學生編號為1並且課程編號不等於3
sql = "SELECT * FROM edu.sc WHERE SID=1 AND NOT CID=3"

查詢結果為:
(1, '01', '80')
(1, '02', '90')

#查詢學生資訊表中,學生編號為1並且課程編號等於3或者3的行
sql = "SELECT * FROM edu.sc WHERE SID=1 AND (CID=3 OR CID=2)"

查詢結果為:
(1, '02', '90')
(1, '03', '99')


按照關鍵字進行排序DESC 降序,ASC升序排列

#查詢學生資訊表中,按照成績進行排序-降序
sql = "SELECT * FROM edu.sc ORDER BY score DESC"

查詢結果為:
(1, '03', '99')
(7, '03', '98')
(1, '02', '90')
(7, '02', '89')
(5, '02', '87')
(1, '01', '80')
(2, '03', '80')
(3, '01', '80')
(3, '02', '80')
(3, '03', '80')
(5, '01', '76')
(2, '01', '70')
(2, '02', '60')
(4, '01', '50')
(6, '03', '34')
(6, '01', '31')
(4, '02', '30')
(4, '03', '20')

#查詢學生資訊表中,SID=4的行,按照成績進行升序排列
sql = "SELECT * FROM edu.sc WHERE SID=4 ORDER BY score ASC"

查詢結果為:
(4, '03', '20')
(4, '02', '30')
(4, '01', '50')


查詢結果不為空的行

查詢sql語言為: SELECT * FROM edu.course
表格全部查詢結果為:
(1, 'English', '02')
(2, 'math', '03')
(3, 'Chinese', '01')
(4, 'physics', '04')
(5, 'geography', '05')
(6, 'French', None)

#查詢教師ID不為空的行
sql = "SELECT * FROM edu.course  WHERE TID IS NOT NULL"

查詢結果為:
(1, 'English', '02')
(2, 'math', '03')
(3, 'Chinese', '01')
(4, 'physics', '04')
(5, 'geography', '05')

#查詢教師ID為空的行
sql = "SELECT * FROM edu.course  WHERE TID IS NULL"

查詢結果為:
(6, 'French', None)