zoukankan      html  css  js  c++  java
  • 作业

    准备数据

    create table book(
    	id int primary key auto_increment,
    	name varchar(16),
    	price int
    );
    create table author(
    	id int primary key auto_increment,
    	name varchar(16)
    );
    create table book_author(
        id int primary key auto_increment,
        book_id int,
        author_id int
    );
    
    id name pirce
    1 葵花宝典 88
    2 九阴真经 78
    3 辟邪剑谱 99
    id name
    1 张全蛋
    2 李诞
    3 寡人
    id book_id author_id
    1 1 1
    2 1 2
    3 1 3
    4 2 1
    5 2 3
    6 3 2
    7 3 2
    2、查看每本书的作者
    3、查看每位作者都有哪些作品
    4、利用pymysql操作数据库实现sql注入效果

    5、利用pymysql实现登陆注册功能

    注册:用户名不能重复

    登陆:展示对应的错误信息(用户名错误,密码错误)

    import pymysql
    
    class Mysql:
        def __init__(self):
            self.conn = pymysql.connect(
                host = '127.0.0.1',
                port = 3306,
                user = 'root',
                password = '123',
                database = 'day41',
                charset = 'utf8'
            )
            self.cursor = self.conn.cursor()
    
        def select(self,name,table):
            sql = "select * from {} where sname = %s".format(table)
            self.cursor.execute(sql,(name,))
            return self.cursor.fetchall()
    
    
    
    
    
    mysql = Mysql()
    res = mysql.select('egon','student')
    print(res)
    
    
    
    
    
    
    conn = pymysql.connect(
        host='127.0.0.1',
        port=3306,
        database='day41',
        charset ='utf8',
        user='root',
        password='123'
    )
    
    cursor = conn.cursor(pymysql.cursors.DictCursor)
    
    sql = 'select * from student'
    
    size = cursor.execute(sql)
    print(size)
    
    data = cursor.fetchone()
    data2 = cursor.fetchone()
    data3 = cursor.fetchall()
    print(data,data2,data3)
    sql = "insert into student(gender,sname,class_id) values('女','yaya','2') "
    cursor.execute(sql)
    conn.commit()
    

    -- 1、查询所有的课程的名称以及对应的任课老师姓名
    -- SELECT course.cname,teacher.tname from course INNER JOIN teacher on course.teacher_id = teacher.tid;
    -- 2、查询学生表中男女生各有多少人
    -- select gender,count(sid) from student GROUP BY gender;

    -- 3、查询物理成绩等于100的学生的姓名
    -- select sname from student where sid in
    -- (SELECT student_id from score where num=100 and course_id in
    -- (SELECT course_id from score INNER JOIN course on score.course_id = course.cid where cname='物理'));
    -- 4、查询平均成绩大于八十分的同学的姓名和平均成绩
    -- select student.sname,t1.avg_num from student INNER JOIN
    -- (SELECT student_id,avg(num)as avg_num from score GROUP BY student_id HAVING avg(num)>80)as t1 on student.sid = t1.student_id;
    -- 5、查询所有学生的学号,姓名,选课数,总成绩
    -- select sid,sname,count_course,sum_num from student INNER JOIN
    -- (select student_id,count(course_id)as count_course,sum(num)as sum_num from score GROUP BY student_id)as t1 on student.sid = t1.student_id;

    -- 6、 查询姓李老师的个数
    -- SELECT COUNT(tname) FROM teacher WHERE tname LIKE '%李%'

    -- 7、 查询没有报李平老师课的学生姓名
    -- select sname from student where sid not in
    -- (select student_id from score where course_id in
    -- (select cid from teacher INNER JOIN course on course.teacher_id = teacher.tid where teacher.tname='李平老师'));
    -- 8、 查询物理课程比生物课程高的学生的学号
    -- select student_id from
    -- (select student_id,num from score where course_id in
    -- (select cid from course where cname='物理'))as t1 INNER JOIN
    -- (select student_id as id,num from score where course_id in
    -- (select cid from course where cname='生物')) as t2
    -- on t1.student_id = t2.id

    -- where t1.num>t2.num;

    -- -- 第二种方法
    -- SELECT
    -- t1.student_id
    -- FROM
    -- (
    -- SELECT
    -- student_id,
    -- num
    -- FROM
    -- score
    -- WHERE
    -- course_id = (
    -- SELECT
    -- cid
    -- FROM
    -- course
    -- WHERE
    -- cname = '物理'
    -- )
    -- ) AS t1
    -- INNER JOIN (
    -- SELECT
    -- student_id,
    -- num
    -- FROM
    -- score
    -- WHERE
    -- course_id = (
    -- SELECT
    -- cid
    -- FROM
    -- course
    -- WHERE
    -- cname = '生物'
    -- )
    -- ) AS t2 ON t1.student_id = t2.student_id
    -- WHERE
    -- t1.num > t2.num;

    -- 9、 查询没有同时选修物理课程和体育课程的学生姓名
    -- select sname from student where sid in
    -- (select student_id from score where course_id in
    -- (select cid from course where cname='体育' or cname='物理') GROUP BY student_id HAVING count(course_id)=1);

    --
    -- 10、查询挂科超过两门(包括两门)的学生姓名和班级
    -- select sname,caption from class INNER JOIN student on class.cid = student.class_id where sid in
    -- (select student_id from score INNER JOIN course on score.course_id = course.cid where score.num<60 GROUP BY student_id having count(course_id)>=2);

    -- 11、查询选修了所有课程的学生姓名
    -- select student.sname from student WHERE sid in
    -- (SELECT student_id from score GROUP BY student_id having count(course_id) = (select count(cid) from course));

    -- 12、查询李平老师教的课程的所有成绩记录
    -- select num from score where course_id in
    -- (select cid from course INNER JOIN teacher on course.teacher_id = teacher.tid where tname = '李平老师');

    -- 13、查询全部学生都选修了的课程号和课程名
    -- select cid,cname from course where cid in
    -- (select course_id from score GROUP BY course_id HAVING count(student_id)=(select count(sid) from student));

    -- 14、查询每门课程被选修的次数
    -- select course_id,count(student_id) from score GROUP BY course_id;

    -- 15、查询之选修了一门课程的学生姓名和学号
    -- select sid,sname from student where sid in
    -- (select student_id from score GROUP BY student_id HAVING count(course_id)=1);
    -- 16、查询所有学生考出的成绩并按从高到低排序(成绩去重)
    -- select distinct num from score ORDER BY num desc;

    -- 17、查询平均成绩大于85的学生姓名和平均成绩
    -- select sname,avg_num from student INNER JOIN
    -- (select student_id,avg(num)as avg_num from score GROUP BY student_id HAVING avg(num)>85)as t1 on student.sid = t1.student_id ;
    -- 18、查询生物成绩不及格的学生姓名和对应生物分数
    select sname,num from student INNER JOIN
    (select student_id,num from score where course_id=

    (select cid from course where cname='生物')and num<60)as t1
    on student.sid = t1.student_id
    Time: 0.001s

    [2019-05-18 18:13:20.11][localhost_3306][000037][MYSQL]
    -- 19、查询在所有选修了李平老师课程的学生中,这些课程(李平老师的课程,不是所有课程)平均成绩最高的学生姓名‘

    select sname from student where sid =(
    -- SELECT student_id from score where course_id in
    -- (select cid from course INNER JOIN teacher on course.teacher_id = teacher.tid where tname='李平老师')group by student_id order by avg(num) desc limit 1);

    -- 20、查询每门课程成绩最好的前两名学生姓名

    SELECT
    score.student_id,
    t3.course_id,
    t3.first_num,
    t3.second_num
    FROM
    score
    INNER JOIN (
    SELECT
    t1.course_id,
    t1.first_num,
    t2.second_num
    FROM
    (
    SELECT
    course_id,
    max(num) first_num
    FROM
    score
    GROUP BY
    course_id
    ) AS t1
    INNER JOIN (
    SELECT
    score.course_id,
    max(num) second_num
    FROM
    score
    INNER JOIN (
    SELECT
    course_id,
    max(num) first_num
    FROM
    score
    GROUP BY
    course_id
    ) AS t ON score.course_id = t.course_id
    WHERE
    score.num < t.first_num
    GROUP BY
    course_id
    ) AS t2 ON t1.course_id = t2.course_id
    ) AS t3 ON score.course_id = t3.course_id
    WHERE
    score.num = t3.second_num
    or score.num = t3.first_num
    ORDER BY
    course_id

    -- 21、查询不同课程但成绩相同的学号,课程号,成绩

    -- 22、查询没学过“叶平”老师课程的学生姓名以及选修的课程名称;

    select student_id,t1.cname from score INNER JOIN
    (select cid,cname from course INNER JOIN teacher on course.teacher_id = teacher.tid where teacher.tname='李平老师')as t1 on score.course_id= t1.cid;

    -- 23、查询所有选修了学号为1的同学选修过的一门或者多门课程的同学学号和姓名;

    -- 24、任课最多的老师中学生单科成绩最高的学生姓名

    select sname from student where student.sid in
    (select student_id from score where num in
    (select max(num) from score where course_id in
    (select cid from course where teacher_id =
    (select tid from teacher INNER JOIN course on course.teacher_id = teacher.tid GROUP BY tid ORDER BY count(teacher_id) desc limit 1))GROUP BY course_id))

  • 相关阅读:
    python 报错:reduce failed to synchronize: device-side assert triggered
    pytorch 分割二分类的两种形式
    Leetcode 104. 二叉树的最大深度
    LeetCode 78. 子集
    python报错:1only batches of spatial targets supported (non-empty 3D tensors) but got targets of size
    PyTorch:The "freeze_support()" line can be omitted if the program is not going to be frozen
    pytorch创建tensor的四种方法
    Leetcode 四数之和
    C++的类和对象
    结构体
  • 原文地址:https://www.cnblogs.com/huangxuanya/p/10894005.html
Copyright © 2011-2022 走看看