MySQL--------单(多)表查询

一、单表查询
 表名:worker-- 表中字段均为中文,比如 部门号 工资 职工号 参加工作 等
CREATE TABLE `worker` (
 `部门号` int(11) NOT NULL,
 `职工号` int(11) NOT NULL,
 `工作时间` date NOT NULL,
 `工资` float(8,2) NOT NULL,
 `政治面貌` varchar(10) NOT NULL DEFAULT '群众',
 `姓名` varchar(20) NOT NULL,
 `出生日期` date NOT NULL,
 PRIMARY KEY (`职工号`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `出生
日期`) VALUES (101, 1001, '2015-5-4', 3500.00, '群众', '张三', '1990-7-1');
INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `出生
日期`) VALUES (101, 1002, '2017-2-6', 3200.00, '团员', '李四', '1997-2-8');
INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `出生
日期`) VALUES (102, 1003, '2011-1-4', 8500.00, '党员', '王亮', '1983-6-8');
INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `出生
日期`) VALUES (102, 1004, '2016-10-10', 5500.00, '群众', '赵六', '1994-9-5');
INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `出生
日期`) VALUES (102, 1005, '2014-4-1', 4800.00, '党员', '钱七', '1992-12-30');
INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `出生
日期`) VALUES (102, 1006, '2017-5-5', 4500.00, '党员', '孙八', '1996-9-2');
(1)创建表数据

mysql8.0 [db_test]>create table worker(                            
    ->     部门号 int(11) not null,
    ->     职工号 int(11) not null,
    ->     工作时间 date not null,
    ->     工资 float(8,2) not null,
    ->     政治面貌 varchar(10) not null default '群众',
    ->     姓名 varchar(20) not null,
    ->     出生日期 date not null,
    ->     primary key'职工号')
    ->     ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
Query OK, 0 rows affected (0.01 sec)
 
 
mysql8.0 [db_test]>INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `出生日期`)
    -> VALUES
    -> (101, 1001, '2015-5-4', 3500.00, '群众', '张三', '1990-7-1'),
    -> (101, 1002, '2017-2-6', 3200.00, '团员', '李四', '1997-2-8'),
    -> (102, 1003, '2011-1-4', 8500.00, '党员', '王亮', '1983-6-8'),
    -> (102, 1004, '2016-10-10', 5500.00, '群众', '赵六', '1994-9-5'),
    -> (102, 1005, '2014-4-1', 4800.00, '党员', '钱七', '1992-12-30'),
    -> (102, 1006, '2017-5-5', 4500.00, '党员', '孙八', '1996-9-2');
Query OK, 5 rows affected (0.00 sec)
Records: 5  Duplicates: 0  Warnings: 0

(2)查看表结构和信息

mysql8.0 [db_test]>desc worker;
+--------------+-------------+------+-----+---------+-------+
| Field        | Type        | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| 部门号       | int(11)     | NO   |     | NULL    |       |
| 职工号       | int(11)     | NO   | PRI | NULL    |       |
| 工作时间     | date        | NO   |     | NULL    |       |
| 工资         | float(8,2)  | NO   |     | NULL    |       |
| 政治面貌     | varchar(10) | NO   |     | 群众    |       |
| 姓名         | varchar(20) | NO   |     | NULL    |       |
| 出生日期     | date        | NO   |     | NULL    |       |
+--------------+-------------+------+-----+---------+-------+
7 rows in set (0.00 sec)
 
mysql8.0 [db_test]>select * from worker;
+-----------+-----------+--------------+---------+--------------+--------+--------------+
| 部门号    | 职工号    | 工作时间     | 工资    | 政治面貌     | 姓名   | 出生日期     |
+-----------+-----------+--------------+---------+--------------+--------+--------------+
|       101 |      1001 | 2015-05-04   | 3500.00 | 群众         | 张三   | 1990-07-01   |
|       101 |      1002 | 2017-02-06   | 3200.00 | 团员         | 李四   | 1997-02-08   |
|       102 |      1003 | 2011-01-04   | 8500.00 | 党员         | 王亮   | 1983-06-08   |
|       102 |      1004 | 2016-10-10   | 5500.00 | 群众         | 赵六   | 1994-09-05   |
|       102 |      1005 | 2014-04-01   | 4800.00 | 党员         | 钱七   | 1992-12-30   |
|       102 |      1006 | 2017-05-05   | 4500.00 | 党员         | 孙八   | 1996-09-02   |
+-----------+-----------+--------------+---------+--------------+--------+--------------+
6 rows in set (0.00 sec)
 

--------显示所有职工的基本信息

mysql8.0 [db_test]>select * from worker;
+-----------+-----------+--------------+---------+--------------+--------+--------------+
| 部门号    | 职工号    | 工作时间     | 工资    | 政治面貌     | 姓名   | 出生日期     |
+-----------+-----------+--------------+---------+--------------+--------+--------------+
|       101 |      1001 | 2015-05-04   | 3500.00 | 群众         | 张三   | 1990-07-01   |
|       101 |      1002 | 2017-02-06   | 3200.00 | 团员         | 李四   | 1997-02-08   |
|       102 |      1003 | 2011-01-04   | 8500.00 | 党员         | 王亮   | 1983-06-08   |
|       102 |      1004 | 2016-10-10   | 5500.00 | 群众         | 赵六   | 1994-09-05   |
|       102 |      1005 | 2014-04-01   | 4800.00 | 党员         | 钱七   | 1992-12-30   |
|       102 |      1006 | 2017-05-05   | 4500.00 | 党员         | 孙八   | 1996-09-02   |
+-----------+-----------+--------------+---------+--------------+--------+--------------+
6 rows in set (0.00 sec)

------------------查询所有职工所属部门的部门号,不显示重复的部门号。  

mysql8.0 [db_test]>select distinct 部门号 from worker;
+-----------+
| 部门号    |
+-----------+
|       101 |
|       102 |
+-----------+
2 rows in set (0.00 sec)

--------------求出所有职工的人数

mysql8.0 [db_test]>select count(*) from worker;
+----------+
| count(*) |
+----------+
|        6 |
+----------+
1 row in set (0.00 sec)

-------------列出最高工和最低工资。

mysql8.0 [db_test]>select max(工资),min(工资) from worker;
+-------------+-------------+
| max(工资)   | min(工资)   |
+-------------+-------------+
|     8500.00 |     3200.00 |
+-------------+-------------+
1 row in set (0.00 sec)

--------------列出职工的平均工资和总工资。

mysql8.0 [db_test]>select sum(工资),avg(工资) from worker;
+-------------+-------------+
| sum(工资)   | avg(工资)   |
+-------------+-------------+
|    30000.00 | 5000.000000 |
+-------------+-------------+
1 row in set (0.00 sec)

----------------创建一个只有职工号、姓名和参加工作的新表,名为工作日期表。

mysql8.0 [db_test]>create table worktime select 职工号,姓名,工作时间 from workeer;
Query OK, 6 rows affected (0.01 sec)
Records: 6  Duplicates: 0  Warnings: 0
 
mysql8.0 [db_test]>desc worktime;
+--------------+-------------+------+-----+---------+-------+
| Field        | Type        | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| 职工号       | int(11)     | NO   |     | NULL    |       |
| 姓名         | varchar(20) | NO   |     | NULL    |       |
| 工作时间     | date        | NO   |     | NULL    |       |
+--------------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

------------------显示所有女职工的年龄。

mysql8.0 [db_test]>select 2024 - year(出生日期) as 年龄 from worker;
+--------+
| 年龄   |
+--------+
|     34 |
|     27 |
|     41 |
|     30 |
|     32 |
|     28 |
+--------+
6 rows in set (0.00 sec)

--------------列出所有姓刘的职工的职工号、姓名和出生日期

mysql8.0 [db_test]>select 职工号,姓名,出生日期 
    -> from worker
    -> where 姓名 like '刘%';
Empty set (0.00 sec)
 

--------------列出1960年以前出生的职工的姓名、参加工作日期。

mysql8.0 [db_test]>select 姓名,工作时间 
    -> from worker
    -> where 出生日期 < '1960-01-01';
Empty set (0.00 sec)

--------------列出工资在1000-2000之间的所有职工姓名。

mysql8.0 [db_test]>select 姓名
    -> from worker
    -> where 工资 between 1000 and 2000;
Empty set (0.00 sec)

--------------列出所有陈姓和李姓的职工姓名

mysql8.0 [db_test]>select 姓名
    -> from worker
    -> where 姓名 like '张%' or 姓名 like '李%';
+--------+
| 姓名   |
+--------+
| 张三   |
| 李四   |
+--------+
2 rows in set (0.00 sec)

-------------列出所有部门号为2和3的职工号、姓名、党员否。

mysql8.0 [db_test]>select 职工号,姓名,政治面貌 from worker where 部门号= '3' orr '2';
+-----------+--------+--------------+
| 职工号    | 姓名   | 政治面貌     |
+-----------+--------+--------------+
|      1001 | 张三   | 群众         |
|      1002 | 李四   | 团员         |
|      1003 | 王亮   | 党员         |
|      1004 | 赵六   | 群众         |
|      1005 | 钱七   | 党员         |
|      1006 | 孙八   | 党员         |
+-----------+--------+--------------+
6 rows in set (0.00 sec)

---------------将职工表worker中的职工按出生的先后顺序排序。

mysql8.0 [db_test]>select 姓名 from worker order by 出生日期;
+--------+
| 姓名   |
+--------+
| 王亮   |
| 张三   |
| 钱七   |
| 赵六   |
| 孙八   |
| 李四   |
+--------+
6 rows in set (0.00 sec)

---------------显示工资最高的前3名职工的职工号和姓名。

mysql8.0 [db_test]>select 职工号,姓名,工资
    -> from worker
    -> order by 工资 desc limit 3;
+-----------+--------+---------+
| 职工号    | 姓名   | 工资    |
+-----------+--------+---------+
|      1003 | 王亮   | 8500.00 |
|      1004 | 赵六   | 5500.00 |
|      1005 | 钱七   | 4800.00 |
+-----------+--------+---------+
3 rows in set (0.00 sec)
 

--------------求出各部门党员的人数

mysql8.0 [db_test]>select 部门号,count(*) 
    -> from worker 
    -> where 政治面貌='党员' group by 部门号;
+-----------+----------+
| 部门号    | count(*) |
+-----------+----------+
|       102 |        3 |
+-----------+----------+
1 row in set (0.00 sec)
 
mysql8.0 [db_test]>

----------------统计各部门的工资和平均工资

mysql8.0 [db_test]>select 部门号,sum(工资),avg(工资)
    -> from worker
    -> group by 部门号;
+-----------+-------------+-------------+
| 部门号    | sum(工资)   | avg(工资)   |
+-----------+-------------+-------------+
|       101 |     6700.00 | 3350.000000 |
|       102 |    23300.00 | 5825.000000 |
+-----------+-------------+-------------+
2 rows in set (0.00 sec)

--------------列出总人数大于4的部门号和总人数

mysql8.0 [db_test]>select 部门号,count(*)
    -> from worker
    -> group by 部门号 having count(*) > 4;
Empty set (0.00 sec)
 

二、多表查询

1.创建student和score表
CREATE  TABLE student (
id  INT(10)  NOT NULL  UNIQUE  PRIMARY KEY ,
name  VARCHAR(20)  NOT NULL ,
sex  VARCHAR(4) ,
birth  YEAR,
department  VARCHAR(20) ,
address  VARCHAR(50)
);
创建score表。SQL代码如下:
CREATE  TABLE score (
id  INT(10)  NOT NULL  UNIQUE  PRIMARY KEY  AUTO_INCREMENT ,
stu_id  INT(10)  NOT NULL ,
c_name  VARCHAR(20) ,
grade  INT(10)
);

mysql8.0 [db_test]>create table student(
    -> id int(10) not null unique primary key,
    -> name varchar(20) not null,
    -> sex varchar(4),
    -> brith year,
    -> department varchar(20),
    -> address varchar(50)
    -> );
Query OK, 0 rows affected (0.01 sec)
 
mysql8.0 [db_test]>create table score(
    -> id int(10) not null unique primary key auto_increment,
    -> stu_id int(10) not null,
    -> c_name varchar(20),
    -> grade int(10)
    -> );
Query OK, 0 rows affected (0.01 sec)

2.为student表和score表增加记录
向student表插入记录的INSERT语句如下:
INSERT INTO student VALUES( 901,'张老大', '男',1985,'计算机系', '北京市海淀区');
INSERT INTO student VALUES( 902,'张老二', '男',1986,'中文系', '北京市昌平区');
INSERT INTO student VALUES( 903,'张三', '女',1990,'中文系', '湖南省永州市');
INSERT INTO student VALUES( 904,'李四', '男',1990,'英语系', '辽宁省阜新市');
INSERT INTO student VALUES( 905,'王五', '女',1991,'英语系', '福建省厦门市');
INSERT INTO student VALUES( 906,'王六', '男',1988,'计算机系', '湖南省衡阳市');

mysql8.0 [db_test]>insert into student(id,name,sex,brith,department,address)
    -> values
    -> ( 901,'张老大', '男',1985,'计算机系', '北京市海淀区'),
    -> ( 902,'张老二', '男',1986,'中文系', '北京市昌平区'),
    -> ( 903,'张三', '女',1990,'中文系', '湖南省永州市'),
    -> ( 904,'李四', '男',1990,'英语系', '辽宁省阜新市'),
    -> ( 905,'王五', '女',1991,'英语系', '福建省厦门市'),
    -> ( 906,'王六', '男',1988,'计算机系', '湖南省衡阳市');
Query OK, 6 rows affected (0.00 sec)
Records: 6  Duplicates: 0  Warnings: 0

向score表插入记录的INSERT语句如下:
INSERT INTO score VALUES(NULL,901, '计算机',98);
INSERT INTO score VALUES(NULL,901, '英语', 80);
INSERT INTO score VALUES(NULL,902, '计算机',65);
INSERT INTO score VALUES(NULL,902, '中文',88);
INSERT INTO score VALUES(NULL,903, '中文',95);
INSERT INTO score VALUES(NULL,904, '计算机',70);
INSERT INTO score VALUES(NULL,904, '英语',92);
INSERT INTO score VALUES(NULL,905, '英语',94);
INSERT INTO score VALUES(NULL,906, '计算机',90);
INSERT INTO score VALUES(NULL,906, '英语',85);

mysql8.0 [db_test]>insert into score(id,stu_id,c_name,grade)
    -> values
    -> (NULL,901, '计算机',98),
    -> (NULL,901, '英语', 80),
    -> (NULL,902, '计算机',65),
    -> (NULL,902, '中文',88),
    -> (NULL,903, '中文',95),
    -> (NULL,904, '计算机',70),
    -> (NULL,904, '英语',92),
    -> (NULL,905, '英语',94),
    -> (NULL,906, '计算机',90),
    -> (NULL,906, '英语',85);
Query OK, 10 rows affected (0.00 sec)
Records: 10  Duplicates: 0  Warnings: 0

3.查询student表的所有记录

mysql8.0 [db_test]>select * from student;
+-----+-----------+------+-------+--------------+--------------------+
| id  | name      | sex  | brith | department   | address            |
+-----+-----------+------+-------+--------------+--------------------+
| 901 | 张老大    | 男   |  1985 | 计算机系     | 北京市海淀区       |
| 902 | 张老二    | 男   |  1986 | 中文系       | 北京市昌平区       |
| 903 | 张三      | 女   |  1990 | 中文系       | 湖南省永州市       |
| 904 | 李四      | 男   |  1990 | 英语系       | 辽宁省阜新市       |
| 905 | 王五      | 女   |  1991 | 英语系       | 福建省厦门市       |
| 906 | 王六      | 男   |  1988 | 计算机系     | 湖南省衡阳市       |
+-----+-----------+------+-------+--------------+--------------------+
6 rows in set (0.00 sec)
 

4.查询student表的第2条到4条记录

mysql8.0 [db_test]>select * 
    -> from student
    -> limit 1,3;
+-----+-----------+------+-------+------------+--------------------+
| id  | name      | sex  | brith | department | address            |
+-----+-----------+------+-------+------------+--------------------+
| 902 | 张老二    | 男   |  1986 | 中文系     | 北京市昌平区       |
| 903 | 张三      | 女   |  1990 | 中文系     | 湖南省永州市       |
| 904 | 李四      | 男   |  1990 | 英语系     | 辽宁省阜新市       |
+-----+-----------+------+-------+------------+--------------------+
3 rows in set (0.00 sec)

5.从student表查询所有学生的学号(id)、姓名(name)和院系(department)的信息

mysql8.0 [db_test]>select id,name,department
    -> from student;
+-----+-----------+--------------+
| id  | name      | department   |
+-----+-----------+--------------+
| 901 | 张老大    | 计算机系     |
| 902 | 张老二    | 中文系       |
| 903 | 张三      | 中文系       |
| 904 | 李四      | 英语系       |
| 905 | 王五      | 英语系       |
| 906 | 王六      | 计算机系     |
+-----+-----------+--------------+
6 rows in set (0.00 sec)

6.从student表中查询计算机系和英语系的学生的信息

mysql8.0 [db_test]>select *  
    -> from student
    -> where department = '计算机系' or department = '英语系';
+-----+-----------+------+-------+--------------+--------------------+
| id  | name      | sex  | brith | department   | address            |
+-----+-----------+------+-------+--------------+--------------------+
| 901 | 张老大    | 男   |  1985 | 计算机系     | 北京市海淀区       |
| 904 | 李四      | 男   |  1990 | 英语系       | 辽宁省阜新市       |
| 905 | 王五      | 女   |  1991 | 英语系       | 福建省厦门市       |
| 906 | 王六      | 男   |  1988 | 计算机系     | 湖南省衡阳市       |
+-----+-----------+------+-------+--------------+--------------------+
4 rows in set (0.00 sec)

7.从student表中查询年龄18~22岁的学生信息

mysql8.0 [db_test]>select * from student where (2024-brith) between 28 and 32; 
Empty set (0.00 sec)

8.从student表中查询每个院系有多少人

mysql8.0 [db_test]>select department,count(*) 人数
    -> from student
    -> group by department;
+--------------+--------+
| department   | 人数   |
+--------------+--------+
| 中文系       |      2 |
| 英语系       |      2 |
| 计算机系     |      2 |
+--------------+--------+
3 rows in set (0.00 sec)

9.从score表中查询每个科目的最高分

mysql8.0 [db_test]>select c_name,max(grade)
    -> from score
    -> group by c_name;
+-----------+------------+
| c_name    | max(grade) |
+-----------+------------+
| 中文      |         95 |
| 英语      |         94 |
| 计算机    |         98 |
+-----------+------------+
3 rows in set (0.00 sec)
 

10.查询李四的试科目(c_name)和考试成绩(grade)

mysql8.0 [db_test]>select c.c_name,c.grade
    -> from student
    -> d,score c where d.id = c.stu_id and d.name = '李四';
+-----------+-------+
| c_name    | grade |
+-----------+-------+
| 计算机    |    70 |
| 英语      |    92 |
+-----------+-------+
2 rows in set (0.00 sec)

11.用连接的方式查询所有学生的信息和考试信息

mysql8.0 [db_test]>select d.id,name,sex,brith,department,address,c_name,grade
    -> from student
    -> d,score c where d.id = c.stu_id;
+-----+-----------+------+-------+--------------+--------------------+-----------+-------+
| id  | name      | sex  | brith | department   | address            | c_name    | grade |
+-----+-----------+------+-------+--------------+--------------------+-----------+-------+
| 901 | 张老大    | 男   |  1985 | 计算机系     | 北京市海淀区       | 计算机    |    98 |
| 901 | 张老大    | 男   |  1985 | 计算机系     | 北京市海淀区       | 英语      |    80 |
| 902 | 张老二    | 男   |  1986 | 中文系       | 北京市昌平区       | 计算机    |    65 |
| 902 | 张老二    | 男   |  1986 | 中文系       | 北京市昌平区       | 中文      |    88 |
| 903 | 张三      | 女   |  1990 | 中文系       | 湖南省永州市       | 中文      |    95 |
| 904 | 李四      | 男   |  1990 | 英语系       | 辽宁省阜新市       | 计算机    |    70 |
| 904 | 李四      | 男   |  1990 | 英语系       | 辽宁省阜新市       | 英语      |    92 |
| 905 | 王五      | 女   |  1991 | 英语系       | 福建省厦门市       | 英语      |    94 |
| 906 | 王六      | 男   |  1988 | 计算机系     | 湖南省衡阳市       | 计算机    |    90 |
| 906 | 王六      | 男   |  1988 | 计算机系     | 湖南省衡阳市       | 英语      |    85 |
+-----+-----------+------+-------+--------------+--------------------+-----------+-------+
10 rows in set (0.00 sec)

12.计算每个学生的总成绩

mysql8.0 [db_test]>select d.id,name,sum(grade) 
    -> from student
    -> d,score c where d.id = c.stu_id group by id;
+-----+-----------+------------+
| id  | name      | sum(grade) |
+-----+-----------+------------+
| 901 | 张老大    |        178 |
| 902 | 张老二    |        153 |
| 903 | 张三      |         95 |
| 904 | 李四      |        162 |
| 905 | 王五      |         94 |
| 906 | 王六      |        175 |
+-----+-----------+------------+
6 rows in set (0.00 sec)

13.计算每个考试科目的平均成绩

mysql8.0 [db_test]>select c_name,avg(grade)
    -> from score
    -> group by c_name;
+-----------+------------+
| c_name    | avg(grade) |
+-----------+------------+
| 中文      |    91.5000 |
| 英语      |    87.7500 |
| 计算机    |    80.7500 |
+-----------+------------+
3 rows in set (0.00 sec)

14.查询计算机成绩低于95的学生信息

mysql8.0 [db_test]>select * from student where id = any(select stu_id from score where c_name = '计算机' and grade < 95);
+-----+-----------+------+-------+--------------+--------------------+
| id  | name      | sex  | brith | department   | address            |
+-----+-----------+------+-------+--------------+--------------------+
| 902 | 张老二    | 男   |  1986 | 中文系       | 北京市昌平区       |
| 904 | 李四      | 男   |  1990 | 英语系       | 辽宁省阜新市       |
| 906 | 王六      | 男   |  1988 | 计算机系     | 湖南省衡阳市       |
+-----+-----------+------+-------+--------------+--------------------+
3 rows in set (0.00 sec)
 

15.查询同时参加计算机和英语考试的学生的信息

mysql8.0 [db_test]>select *
    -> from student
    -> where id = any(
    -> select stu_id
    -> from score
    -> where c_name = '计算机' and c_name='英语');
Empty set (0.00 sec)

16.将计算机考试成绩按从高到低进行排序

mysql8.0 [db_test]>select stu_id,grade 
    -> from score
    -> where c_name = '计算机' order by grade desc;
+--------+-------+
| stu_id | grade |
+--------+-------+
|    901 |    98 |
|    906 |    90 |
|    904 |    70 |
|    902 |    65 |
+--------+-------+
4 rows in set (0.00 sec)

17.从student表和score表中查询出学生的学号,然后合并查询结果

mysql8.0 [db_test]>select id 
    -> from student
    -> union select stu_id 
    -> from score;
+-----+
| id  |
+-----+
| 901 |
| 902 |
| 903 |
| 904 |
| 905 |
| 906 |
+-----+
6 rows in set (0.00 sec)

18.查询姓张或者姓王的同学的姓名、院系和考试科目及成绩

mysql8.0 [db_test]>select name,department,c_name,grade
    -> from student,score
    -> where student.id=score.stu_id and (name like '张%' or name like '王%'); 
+-----------+--------------+-----------+-------+
| name      | department   | c_name    | grade |
+-----------+--------------+-----------+-------+
| 张老大    | 计算机系     | 计算机    |    98 |
| 张老大    | 计算机系     | 英语      |    80 |
| 张老二    | 中文系       | 计算机    |    65 |
| 张老二    | 中文系       | 中文      |    88 |
| 张三      | 中文系       | 中文      |    95 |
| 王五      | 英语系       | 英语      |    94 |
| 王六      | 计算机系     | 计算机    |    90 |
| 王六      | 计算机系     | 英语      |    85 |
+-----------+--------------+-----------+-------+
8 rows in set (0.00 sec)
 

19.查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩

mysql8.0 [db_test]>select name,brith,department,c_name,grade
    -> from student,score
    -> where student.id = score.stu_id and address like '湖南%';
+--------+-------+--------------+-----------+-------+
| name   | brith | department   | c_name    | grade |
+--------+-------+--------------+-----------+-------+
| 张三   |  1990 | 中文系       | 中文      |    95 |
| 王六   |  1988 | 计算机系     | 计算机    |    90 |
| 王六   |  1988 | 计算机系     | 英语      |    85 |
+--------+-------+--------------+-----------+-------+
3 rows in set (0.00 sec)
  • 24
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值