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()
增删改查

猜你喜欢

转载自www.cnblogs.com/Mr-Feng/p/10919965.html