• 16.多表联合查询


    建立几个关联表,实现多表连表查询,就需要有关联表及数据

    16.1 创建学生表

    mysql> create database oldboy;
    Query OK, 1 row affected (0.01 s
    mysql> use oldboy
    Database changed
    创建学生表
    create table student(
    Sno int(10) NOT NULL COMMENT '学号',
    Sname varchar(16) NOT NULL COMMENT '姓名',
    Ssex char(2) NOT NULL COMMENT '性别',
    Sage tinyint(2) NOT NULL default '0' COMMENT '学生年龄',
    Sdept varchar(16) default NULL COMMENT '学生所在系别',
    PRIMARY KEY (Sno) ,
    key index_Sname (Sname)
    );

    16.2 在学生表里插入数据

    insert into student values(0001,'宏志','',30,'计算机网络');
    insert into student values(0002,'王硕','',30,'computer application');
    insert into student values(0003,'oldboy','',28,'物流管理');
    insert into student values(0004,'脉动','',29,'computer application');
    insert into student values(0005,'oldgirl','',26,'计算机科学与技术');
    insert into student values(0006,'莹莹','',22,'护士');

    16.3 创建课程表

    create table course(
    
    Cno int(10) NOT NULL COMMENT '课程号',
    Cname varchar(16) NOT NULL COMMENT '课程名',
    Ccredit tinyint(2) NOT NULL COMMENT '学分',
    PRIMARY KEY (Cno)
    );

    16.4 在课程表里插入数据

    insert into course values(1001,'linux 中高级运维','3');
    insert into course values(1002,'linux 中高级架构师','5');
    insert into course values(1003,'mysql 高级 Dba','4');
    insert into course values(1004,'python 运维开发','4');
    insert into course values(1005,'java web 开发','3');

    16.5 创建选课表

    create table sc(
    Scid int(12) not null auto_increment COMMENT '主键',
    Cno int(10) not null COMMENT '课程号',
    Sno int(10) not null COMMENT '学号',
    Grade tinyint(2) not null COMMENT '学生成绩',
    PRIMARY KEY(Scid)
    );

    16.6 在选课表里加入数据

    insert into sc(Sno,Cno,Grade) values(0001,1001,4);
    insert into sc(Sno,Cno,Grade) values(0001,1002,3);
    insert into sc(Sno,Cno,Grade) values(0001,1003,1);
    insert into sc(Sno,Cno,Grade) values(0001,1004,6);
    insert into sc(Sno,Cno,Grade) values(0002,1001,3);
    insert into sc(Sno,Cno,Grade) values(0002,1002,2);
    insert into sc(Sno,Cno,Grade) values(0002,1003,2);
    insert into sc(Sno,Cno,Grade) values(0002,1004,8);
    insert into sc(Sno,Cno,Grade) values(0003,1001,4);
    insert into sc(Sno,Cno,Grade) values(0003,1002,4);
    insert into sc(Sno,Cno,Grade) values(0003,1003,2);
    insert into sc(Sno,Cno,Grade) values(0003,1004,8);
    insert into sc(Sno,Cno,Grade) values(0004,1001,1);
    insert into sc(Sno,Cno,Grade) values(0004,1002,1);
    insert into sc(Sno,Cno,Grade) values(0004,1003,2);
    insert into sc(Sno,Cno,Grade) values(0004,1004,3);
    insert into sc(Sno,Cno,Grade) values(0005,1001,5);
    insert into sc(Sno,Cno,Grade) values(0005,1002,3);
    insert into sc(Sno,Cno,Grade) values(0005,1003,2);
    insert into sc(Sno,Cno,Grade) values(0005,1004,9);

    16.6 联表查询命令

    mysql> select student.Sno,student.Sname,course.Cname,sc.Grade from student,course,sc where
    student.Sno=sc.Sno and course.Cno=sc.Cno order by Sno;
    +-----+---------+-------------------------+-------+
    | Sno | Sname | Cname | Grade |
    +-----+---------+-------------------------+-------+
    | 1 | 宏志 | mysql 高级 Dba | 1 |
    | 1 | 宏志 | python 运维开发 | 6 |
    | 1 | 宏志 | linux 中高级运维 | 4 |
    | 1 | 宏志 | linux 中高级架构师 | 3 |
    | 2 | 王硕 | linux 中高级运维 | 3 |
    | 2 | 王硕 | linux 中高级架构师 | 2 |
    | 2 | 王硕 | mysql 高级 Dba | 2 |
    | 2 | 王硕 | python 运维开发 | 8 |
    | 3 | oldboy | mysql 高级 Dba | 2 |
    | 3 | oldboy | python 运维开发 | 8 |
    | 3 | oldboy | linux 中高级运维 | 4 |
    | 3 | oldboy | linux 中高级架构师 | 4 |
    | 4 | 脉动 | linux 中高级运维 | 1 |
    | 4 | 脉动 | linux 中高级架构师 | 1 |
    | 4 | 脉动 | mysql 高级 Dba | 2 |
    | 4 | 脉动 | python 运维开发 | 3 |
    | 5 | oldgirl | python 运维开发 | 9 |
    | 5 | oldgirl | linux 中高级运维 | 5 |
    | 5 | oldgirl | linux 中高级架构师 | 3 |
    | 5 | oldgirl | mysql 高级 Dba | 2 |
    +-----+---------+-------------------------+-------+
    20 rows in set (0.00 sec)
  • 相关阅读:
    An EventDriven Classifier for Spiking Neural Networks Fed with Synthetic or Dynamic Vision Sensor Data
    Oneshot learning with spiking neural networks
    vs2010使用WebDeploymentSetup.msi发布website网站
    XhEditor上传
    导出(winform) word
    N个富文本编辑器/基于Web的HTML编辑器
    jquery实现ajax,返回json数据
    JavaScript_Date对象说明
    系统缓存全解析3:页面局部缓存
    正则表达式[转]
  • 原文地址:https://www.cnblogs.com/hackerlin/p/12539756.html
Copyright © 2020-2023  润新知