sql語句及pymysql


 

sql語句示例:

7、查詢學過“001”並且也學過編號“002”課程的同學的學號、姓名;
-- select score.student_id,student.sname from score
--
-- left join student on score.student_id=student.sid
--
-- where course_id =1 or course_id =2 GROUP BY student_id HAVING count(course_id) > 1


8、查詢學過“葉平”老師所教的所有課的同學的學號、姓名;
-- select student_id from score where course_id in (
-- select cid from course left JOIN teacher on course.teacher_id = teacher.tid where teacher.tname = "李平老師"
-- ) GROUP BY student_id having count(course_id) = (select count(cid) from course left JOIN teacher on course.teacher_id = teacher.tid where teacher.tname = "李平老師")
--
--
10、查詢有課程成績小於60分的同學的學號、姓名;
-- select student_id from score where num < 60 GROUP BY student_id
-- select DISTINCT student_id from score where num < 60

-- 查詢沒有學全所有課的同學的學號、姓名;

11、查詢沒有學全所有課的同學的學號、姓名;
-- select student_id,count(1) from score GROUP BY student_id HAVING count(1) < (select count(cid) from course);
--

-- 12、查詢至少有一門課與學號為“001”的同學所學相同的同學的學號和姓名;
-- select course_id from score where student_id = 1;
-- select student_id from score where student_id != 1 and course_id in (select course_id from score where student_id = 1) GROUP BY student_id

-- 13、查詢至少學過學號為“001”同學所有課的其他同學學號和姓名;
-- select course_id from score where student_id = 1;
-- select student_id,count(1) from score where student_id != 1 and course_id in (select course_id from score where student_id = 1) GROUP BY student_id HAVING count(1) = (select count(course_id) from score where student_id = 1)


-- 14、查詢和“002”號的同學學習的課程完全相同的其他同學學號和姓名;

-- 獲取和方少偉選課個數相同的通許
-- select count(1) from score where student_id = 1;
--

-- select student_id from score where student_id in (
-- select student_id from score where student_id !=1 GROUP BY student_id HAVING count(1) = (select count(1) from score where student_id = 1)
-- ) and course_id in (select course_id from score where student_id = 1) GROUP BY student_id HAVING count(1) = (select count(1) from score where student_id = 1)
--
--
-- insert into tb(student_id,course_id,num)
--
-- select student_id,2,(SELECT AVG(num) from score where course_id = 2) from score where course_id != 2

-- 17、按平均成績從低到高 顯示所有學生的“語文”、“數學”、“英語”三門的課程成績,按如下形式顯示: 學生ID,語文,數學,英語,有效課程數,有效平均分;
-- 1 90 80 99
-- 2 90 80 99
-- SELECT
-- student_id,
-- (select num from score as s2 where s2.student_id=s1.student_id and course_id = 1) as 語文,
-- (select num from score as s2 where s2.student_id=s1.student_id and course_id = 2) as 數學,
-- (select num from score as s2 where s2.student_id=s1.student_id and course_id = 3) as 英語
-- from score as s1;
--
-- 18、查詢各科成績最高和最低的分:以如下形式顯示:課程ID,最高分,最低分;
-- select course_id,max(num),min(num),min(num)+1,case when min(num) <10 THEN 0 ELSE min(num) END as c from score GROUP BY course_id

-- 19、按各科平均成績從低到高和及格率的百分數從高到低順序;


select course_id,avg(num),sum(case when num <60 THEN 0 ELSE 1 END),sum(1),sum(case when num <60 THEN 0 ELSE 1 END)/sum(1) as jgl from score GROUP BY course_id order by AVG(num) asc,jgl desc;

 

pymysql模塊:
pip3 install pymysql -i https://pypi.douban.com/simple
Python模塊:對數據庫進行操作(SQL語句)

1. Python實現用戶登錄
2. MySQL保存數據


- 連接、關閉(游標)
- execute() -- SQL注入
- 增刪改: conn.commit()
- fetchone fetchall
- 獲取插入數據自增ID

錯誤示例:容易被注入sql語句

import pymysql

user = input("username:")
pwd = input("password:")

conn = pymysql.connect(host="localhost",user='root',password='',database="db666")
cursor = conn.cursor()
sql = "select * from userinfo where username='%s' and password='%s'" %(user,pwd,)
# select * from userinfo where username='uu' or 1=1 -- ' and password='%s'
cursor.execute(sql)
result = cursor.fetchone()
cursor.close()
conn.close()

if result:
    print('登錄成功')
else:
    print('登錄失敗')
erro example

增刪改查:

import pymysql

# 增加,刪,改

# 連接數據庫********************************************************************
conn = pymysql.connect(host="localhost",user='root',password='',database="db666")
cursor = conn.cursor()
# sql語句***********************************************************************
sql = "insert into userinfo(username,password) values('root','123123')"
# 受影響的行數
r = cursor.execute(sql)     # 執行sql語句,返回值是受影響的行數
#  ******
conn.commit()               # 如果是增,刪,改的話commit將修改數據提交到數據庫
# 關閉連接*********************************************************************
cursor.close()
conn.close()

conn = pymysql.connect(host="localhost",user='root',password='',database="db666")
cursor = conn.cursor()

# sql = "insert into userinfo(username,password) values(%s,%s)"
# cursor.execute(sql,(user,pwd,))                   # 執行一行

sql = "insert into userinfo(username,password) values(%s,%s)"

# 受影響的行數
r = cursor.executemany(sql,[('egon','sb'),('laoyao','BS')])     # 執行多行
#  ******
conn.commit()
cursor.close()
conn.close()




#
conn = pymysql.connect(host="localhost",user='root',password='',database="db666")
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
sql = "select * from userinfo"
cursor.execute(sql)

cursor.scroll(1,mode='relative')  # 相對當前位置移動
cursor.scroll(2,mode='absolute') # 相對絕對位置移動
result = cursor.fetchone()      # 只顯示第一行
print(result)
result = cursor.fetchone()
print(result)
result = cursor.fetchone()
print(result)
result = cursor.fetchall()   # 顯示全部
print(result)


result = cursor.fetchmany(4)
print(result)
cursor.close()
conn.close()




# 新插入數據的自增ID: cursor.lastrowid
import pymysql

conn = pymysql.connect(host="localhost",user='root',password='',database="db666")
cursor = conn.cursor()
sql = "insert into userinfo(username,password) values('asdfasdf','123123')"
cursor.execute(sql)
conn.commit()
print(cursor.lastrowid)     # 自增ID
cursor.close()
conn.close()
增刪改查

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM