sql語句示例:mysql
七、查詢學過「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) > 1sql
八、查詢學過「葉平」老師所教的全部課的同窗的學號、姓名;
-- 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 = "李平老師")
--
--
十、查詢有課程成績小於60分的同窗的學號、姓名;
-- select student_id from score where num < 60 GROUP BY student_id
-- select DISTINCT student_id from score where num < 60數據庫
-- 查詢沒有學全全部課的同窗的學號、姓名;ide
十一、查詢沒有學全全部課的同窗的學號、姓名;
-- select student_id,count(1) from score GROUP BY student_id HAVING count(1) < (select count(cid) from course);
-- 學習
-- 十二、查詢至少有一門課與學號爲「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_idfetch
-- 1三、查詢至少學過學號爲「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)spa
-- 1四、查詢和「002」號的同窗學習的課程徹底相同的其餘同窗學號和姓名;code
-- 獲取和方少偉選課個數相同的通許
-- select count(1) from score where student_id = 1;
-- blog
-- 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 != 2ip
-- 1七、按平均成績從低到高 顯示全部學生的「語文」、「數學」、「英語」三門的課程成績,按以下形式顯示: 學生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;
--
-- 1八、查詢各科成績最高和最低的分:以以下形式顯示:課程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
-- 1九、按各科平均成績從低到高和及格率的百分數從高到低順序;
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('登陸失敗')
增刪改查:
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()