赞
踩
目录
1.left join:以左表为标准,查询输出左表中没有的字段信息
1.right join:以右表为标准,查询输出右表中没有的字段信息
select (*|指定字段) from 表1 inner join 表2 on 匹配条件;
可以配合where语句、order by语句、逻辑语句等一起使用
- --t5的id和t6的id具有外键关系
- mysql8.0 [SLB]>select * from t5;
- +----+---------------+-----------+
- | id | name | major |
- +----+---------------+-----------+
- | 1 | sulibao | dianshang |
- | 2 | lixinjin | dianshang |
- | 3 | zhanghongyuan | jike |
- +----+---------------+-----------+
- 3 rows in set (0.00 sec)
-
- mysql8.0 [SLB]>select * from t6;
- +------+--------+------+
- | age | weight | id |
- +------+--------+------+
- | 18 | 75.5 | 1 |
- | 19 | 75.5 | 2 |
- | 25 | 90 | 3 |
- +------+--------+------+
- 3 rows in set (0.00 sec)
-
- mysql8.0 [SLB]>select * from t5 inner join t6 on t5.id=t6.id; --联合查询所有字段
- +----+---------------+-----------+------+--------+------+
- | id | name | major | age | weight | id |
- +----+---------------+-----------+------+--------+------+
- | 1 | sulibao | dianshang | 18 | 75.5 | 1 |
- | 2 | lixinjin | dianshang | 19 | 75.5 | 2 |
- | 3 | zhanghongyuan | jike | 25 | 90 | 3 |
- +----+---------------+-----------+------+--------+------+
- 3 rows in set (0.00 sec)
-
- mysql8.0 [SLB]>select t5.id,t5.name,t5.major,t6.age,t6.weight from t5 inner join t6 on t
- 5.id=t6.id;
- --联合查询指定字段
- +----+---------------+-----------+------+--------+
- | id | name | major | age | weight |
- +----+---------------+-----------+------+--------+
- | 1 | sulibao | dianshang | 18 | 75.5 |
- | 2 | lixinjin | dianshang | 19 | 75.5 |
- | 3 | zhanghongyuan | jike | 25 | 90 |
- +----+---------------+-----------+------+--------+
- 3 rows in set (0.00 sec)
-
- mysql8.0 [SLB]>select a.id,a.name,a.major,b.age,b.weight from t5 a inner join t6 b on a.id=b.id;
- --也可以为名称较长的表取别名
- +----+---------------+-----------+------+--------+
- | id | name | major | age | weight |
- +----+---------------+-----------+------+--------+
- | 1 | sulibao | dianshang | 18 | 75.5 |
- | 2 | lixinjin | dianshang | 19 | 75.5 |
- | 3 | zhanghongyuan | jike | 25 | 90 |
- +----+---------------+-----------+------+--------+
- 3 rows in set (0.00 sec)
-
select (*|指定字段) from 左表 left join 从表 on 匹配条件;
可以配合where语句、order by语句、逻辑语句等一起使用
(1)
- mysql8.0 [SLB]>select * from t5;
- +----+---------------+-----------+
- | id | name | major |
- +----+---------------+-----------+
- | 1 | sulibao | dianshang |
- | 2 | lixinjin | dianshang |
- | 3 | zhanghongyuan | jike |
- +----+---------------+-----------+
- 3 rows in set (0.00 sec)
-
- mysql8.0 [SLB]>select * from t6;
- +------+--------+------+
- | age | weight | id |
- +------+--------+------+
- | 18 | 75.5 | 1 |
- | 19 | 75.5 | 2 |
- | 25 | 90 | 3 |
- +------+--------+------+
- 3 rows in set (0.00 sec)
-
- mysql8.0 [SLB]>select * from t5 left join t6 on t5.id=t6.id;
- +----+---------------+-----------+------+--------+------+
- | id | name | major | age | weight | id |
- +----+---------------+-----------+------+--------+------+
- | 1 | sulibao | dianshang | 18 | 75.5 | 1 |
- | 2 | lixinjin | dianshang | 19 | 75.5 | 2 |
- | 3 | zhanghongyuan | jike | 25 | 90 | 3 |
- +----+---------------+-----------+------+--------+------+
- 3 rows in set (0.01 sec)
-
- mysql8.0 [SLB]>select * from t6 left join t5 on t5.id=t6.id;
- +------+--------+------+------+---------------+-----------+
- | age | weight | id | id | name | major |
- +------+--------+------+------+---------------+-----------+
- | 18 | 75.5 | 1 | 1 | sulibao | dianshang |
- | 19 | 75.5 | 2 | 2 | lixinjin | dianshang |
- | 25 | 90 | 3 | 3 | zhanghongyuan | jike |
- +------+--------+------+------+---------------+-----------+
- 3 rows in set (0.00 sec)
(2)
左表有的字段而从表没有对应内容则映射为null
- mysql8.0 [SLB]>insert t5 values(4,"niexincheng","dianshang");
- Query OK, 1 row affected (0.00 sec)
-
- mysql8.0 [SLB]>select * from t5 left join t6 on t5.id=t6.id;
- +----+---------------+-----------+------+--------+------+
- | id | name | major | age | weight | id |
- +----+---------------+-----------+------+--------+------+
- | 1 | sulibao | dianshang | 18 | 75.5 | 1 |
- | 2 | lixinjin | dianshang | 19 | 75.5 | 2 |
- | 3 | zhanghongyuan | jike | 25 | 90 | 3 |
- | 4 | niexincheng | dianshang | NULL | NULL | NULL |
- +----+---------------+-----------+------+--------+------+
- 4 rows in set (0.00 sec)
左表没有的字段而从表有则不查询这些字段内容
- mysql8.0 [SLB]>select * from t6 left join t5 on t5.id=t6.id;
- +------+--------+------+------+---------------+-----------+
- | age | weight | id | id | name | major |
- +------+--------+------+------+---------------+-----------+
- | 18 | 75.5 | 1 | 1 | sulibao | dianshang |
- | 19 | 75.5 | 2 | 2 | lixinjin | dianshang |
- | 25 | 90 | 3 | 3 | zhanghongyuan | jike |
- +------+--------+------+------+---------------+-----------+
- 3 rows in set (0.00 sec)
用法同左连接类似
- mysql8.0 [SLB]>select * from t6 right join t5 on t5.id=t6.id;
- +------+--------+------+----+---------------+-----------+
- | age | weight | id | id | name | major |
- +------+--------+------+----+---------------+-----------+
- | 18 | 75.5 | 1 | 1 | sulibao | dianshang |
- | 19 | 75.5 | 2 | 2 | lixinjin | dianshang |
- | 25 | 90 | 3 | 3 | zhanghongyuan | jike |
- | NULL | NULL | NULL | 4 | niexincheng | dianshang |
- +------+--------+------+----+---------------+-----------+
- 4 rows in set (0.01 sec)
-
- mysql8.0 [SLB]>select * from t5 right join t6 on t5.id=t6.id;
- +------+---------------+-----------+------+--------+------+
- | id | name | major | age | weight | id |
- +------+---------------+-----------+------+--------+------+
- | 1 | sulibao | dianshang | 18 | 75.5 | 1 |
- | 2 | lixinjin | dianshang | 19 | 75.5 | 2 |
- | 3 | zhanghongyuan | jike | 25 | 90 | 3 |
- +------+---------------+-----------+------+--------+------+
- 3 rows in set (0.00 sec)
示例:yl表中查询yage<lage的所有信息,yl表中查询yage>lage的所有信息
- mysql8.0 [SLB]>select y1.* from yl y1 join yl y2 on y1.lname=y2.lname and y1.yage<y2.lage;
- +----------+------+------+-------+
- | name | yage | lage | lname |
- +----------+------+------+-------+
- | sulibao | 20 | 25 | lh |
- | lixinjin | 21 | 29 | cq |
- +----------+------+------+-------+
- 2 rows in set (0.00 sec)
-
- mysql8.0 [SLB]>select * from yl;
- +-------------+------+------+-------+
- | name | yage | lage | lname |
- +-------------+------+------+-------+
- | sulibao | 20 | 25 | lh |
- | lixinjin | 21 | 29 | cq |
- | niexincheng | 28 | 25 | zxw |
- +-------------+------+------+-------+
- 3 rows in set (0.00 sec)
-
- mysql8.0 [SLB]>select y1.* from yl y1 join yl y2 on y1.lname=y2.lname and y1.yage>y2.lage;
- +-------------+------+------+-------+
- | name | yage | lage | lname |
- +-------------+------+------+-------+
- | niexincheng | 28 | 25 | zxw |
- +-------------+------+------+-------+
- 1 row in set (0.01 sec)
(1)创建数据库,删除数据库,查询创建数据库的语句,使用数据库,查询当前默认的数据库以及使用的编码方式校验规则
- mysql8.0 [S1]>create database S1;
- mysql8.0 [S1]>show create database S1;
- +----------+------------------------------------------------------------------------------------------------------------------------------+
- | Database | Create Database |
- +----------+------------------------------------------------------------------------------------------------------------------------------+
- | S1 | CREATE DATABASE `S1` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */ |
- +----------+------------------------------------------------------------------------------------------------------------------------------+
- 1 row in set (0.00 sec)
- mysql8.0 [S1]>use S1;
- mysql8.0 [S1]>show variables like 'character%';
- +--------------------------+----------------------------+
- | Variable_name | Value |
- +--------------------------+----------------------------+
- | character_set_client | utf8mb4 |
- | character_set_connection | utf8mb4 |
- | character_set_database | utf8mb4 |
- | character_set_filesystem | binary |
- | character_set_results | utf8mb4 |
- | character_set_server | utf8mb4 |
- | character_set_system | utf8mb3 |
- | character_sets_dir | /usr/share/mysql/charsets/ |
- +--------------------------+----------------------------+
- 8 rows in set (0.01 sec)
-
- mysql8.0 [S1]>show variables like 'collation%';
- +----------------------+--------------------+
- | Variable_name | Value |
- +----------------------+--------------------+
- | collation_connection | utf8mb4_0900_ai_ci |
- | collation_database | utf8mb4_0900_ai_ci |
- | collation_server | utf8mb4_0900_ai_ci |
- +----------------------+--------------------+
- 3 rows in set (0.01 sec)
- mysql8.0 [S1]>drop database S1;
- Query OK, 0 rows affected (0.03 sec)
-
- mysql8.0 [(none)]>
(2)在一张表中定义多个字段,要使用今天提到的所有的数据类型(数字,文本,日期)
- mysql8.0 [SLB]>create table t6 (
- -> name char(10),
- -> email varchar(30),
- -> address tinytext,
- -> classmates text,
- -> mess blob,
- -> mateaddress mediumblob,
- -> data mediumblob,
- -> phonelocation longtext,
- -> peopleid longblob,
- -> sex enum('male','female'),
- -> clothcolor set('red','blue','black'),
- -> num tinyint(127),
- -> phonelast smallint(255),
- -> port mediumint(255),
- -> id int(255),
- -> idcardid bigint(255),
- -> score float(100,9),
- -> scoresum double(100,0),
- -> averscore decimal(65,0),
- -> today date,
- -> hcomeback datetime,
- -> log timestamp,
- -> nowtime time,
- -> thisyear year);
- Query OK, 0 rows affected, 7 warnings (0.07 sec)
-
- mysql8.0 [SLB]>desc t6;
- +---------------+---------------------------+------+-----+---------+-------+
- | Field | Type | Null | Key | Default | Extra |
- +---------------+---------------------------+------+-----+---------+-------+
- | name | char(10) | YES | | NULL | |
- | email | varchar(30) | YES | | NULL | |
- | address | tinytext | YES | | NULL | |
- | classmates | text | YES | | NULL | |
- | mess | blob | YES | | NULL | |
- | mateaddress | mediumblob | YES | | NULL | |
- | data | mediumblob | YES | | NULL | |
- | phonelocation | longtext | YES | | NULL | |
- | peopleid | longblob | YES | | NULL | |
- | sex | enum('male','female') | YES | | NULL | |
- | clothcolor | set('red','blue','black') | YES | | NULL | |
- | num | tinyint | YES | | NULL | |
- | phonelast | smallint | YES | | NULL | |
- | port | mediumint | YES | | NULL | |
- | id | int | YES | | NULL | |
- | idcardid | bigint | YES | | NULL | |
- | score | float(100,9) | YES | | NULL | |
- | scoresum | double(100,0) | YES | | NULL | |
- | averscore | decimal(65,0) | YES | | NULL | |
- | today | date | YES | | NULL | |
- | hcomeback | datetime | YES | | NULL | |
- | log | timestamp | YES | | NULL | |
- | nowtime | time | YES | | NULL | |
- | thisyear | year | YES | | NULL | |
- +---------------+---------------------------+------+-----+---------+-------+
- 24 rows in set (0.01 sec)
-
题1
表信息
- 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 [SLB]>select * from worker;
- +-----------+-----------+--------------+---------+--------------+--
- | 部门号 | 职工号 | 工作时间 | 工资 | 政治面貌 | 姓
- +-----------+-----------+--------------+---------+--------------+--
- | 101 | 1001 | 2015-05-04 | 3500.00 | 群众 | 张
- | 101 | 1002 | 2017-02-06 | 3200.00 | 团员 | 李
- | 102 | 1003 | 2011-01-04 | 8500.00 | 党员 | 王
- | 102 | 1004 | 2016-10-10 | 5500.00 | 群众 | 赵
- | 102 | 1005 | 2014-04-01 | 4800.00 | 党员 | 钱
- | 102 | 1006 | 2017-05-05 | 4500.00 | 党员 | 孙
- +-----------+-----------+--------------+---------+--------------+--
- 6 rows in set (0.00 sec)
(2)查询所有职工所属部门的部门号,不显示重复的部门号
- mysql8.0 [SLB]>select distinct 部门号 from worker;
- +-----------+
- | 部门号 |
- +-----------+
- | 101 |
- | 102 |
- +-----------+
- 2 rows in set (0.02 sec)
(3)求出所有职工的人数
- mysql8.0 [SLB]>select count(姓名) from worker;;
- +---------------+
- | count(姓名) |
- +---------------+
- | 6 |
- +---------------+
- 1 row in set (0.02 sec)
(4)列出最高工资和最低工资
- mysql8.0 [SLB]>select max(工资),min(工资) from worker;
- +-------------+-------------+
- | max(工资) | min(工资) |
- +-------------+-------------+
- | 8500.00 | 3200.00 |
- +-------------+-------------+
- 1 row in set (0.01 sec)
(5)列出职工的平均工资和总工资
- mysql8.0 [SLB]>select avg(工资),sum(工资) from worker;
- +-------------+-------------+
- | avg(工资) | sum(工资) |
- +-------------+-------------+
- | 5000.000000 | 30000.00 |
- +-------------+-------------+
- 1 row in set (0.01 sec)
(6)创建一个只有职工号、姓名和参加工作日期的新表,名为工作日期表
- mysql8.0 [SLB]>create table 工作日期表 select 职工号,姓名,工作时间 from worker;
- Query OK, 6 rows affected (0.05 sec)
- Records: 6 Duplicates: 0 Warnings: 0
- mysql8.0 [SLB]>select * from 工作日期表;
- +-----------+--------+--------------+
- | 职工号 | 姓名 | 工作时间 |
- +-----------+--------+--------------+
- | 1001 | 张三 | 2015-05-04 |
- | 1002 | 李四 | 2017-02-06 |
- | 1003 | 王亮 | 2011-01-04 |
- | 1004 | 赵六 | 2016-10-10 |
- | 1005 | 钱七 | 2014-04-01 |
- | 1006 | 孙八 | 2017-05-05 |
- +-----------+--------+--------------+
- 6 rows in set (0.00 sec)
(7)显示所有女职工的年龄,手动添加
- mysql8.0 [SLB]>select * from worker;
- +-----------+-----------+--------------+---------+--------------+--------+--------------+--------+--------+
- | 部门号 | 职工号 | 工作时间 | 工资 | 政治面貌 | 姓名 | 出生日期 | 性别 | 年龄 |
- +-----------+-----------+--------------+---------+--------------+--------+--------------+--------+--------+
- | 101 | 1001 | 2015-05-04 | 3500.00 | 群众 | 张三 | 1990-07-01 | 女 | 29 |
- | 101 | 1002 | 2017-02-06 | 3200.00 | 团员 | 李四 | 1997-02-08 | 男 | 33 |
- | 102 | 1003 | 2011-01-04 | 8500.00 | 党员 | 王亮 | 1983-06-08 | 男 | 25 |
- | 102 | 1004 | 2016-10-10 | 5500.00 | 群众 | 赵六 | 1994-09-05 | 女 | 19 |
- | 102 | 1005 | 2014-04-01 | 4800.00 | 党员 | 钱七 | 1992-12-30 | 女 | 18 |
- | 102 | 1006 | 2017-05-05 | 4500.00 | 党员 | 孙八 | 1996-09-02 | 女 | 29 |
- +-----------+-----------+--------------+---------+--------------+--------+--------------+--------+--------+
- 6 rows in set (0.01 sec)
- mysql8.0 [SLB]>select 年龄 from worker where 性别='女';
- +--------+
- | 年龄 |
- +--------+
- | 29 |
- | 19 |
- | 18 |
- | 29 |
- +--------+
- 4 rows in set (0.00 sec)
(8)列出所有姓刘的职工的职工号、姓名和出生日期
- mysql8.0 [SLB]>select 职工号,姓名,出生日期 from worker where 姓 名 regexp '^刘';
- Empty set (0.01 sec)
(9)列出1960年以前出生的职工的姓名、参加工作日期
- mysql8.0 [SLB]>select 姓名,工作时间 from worker where 出生日期 < 1960-01-01;
- Empty set, 1 warning (0.00 sec)
(10)列出工资在1000-2000之间的所有职工姓名
- mysql8.0 [SLB]>select 姓名 from worker where 工资 >= 1000 and 工资 <= 2000;
- Empty set (0.00 sec)
(11)列出所有陈姓和李姓的职工姓名
- mysql8.0 [SLB]>select 姓名 from worker where 姓名 regexp '^陈' or 姓名 regexp '^李';
- +--------+
- | 姓名 |
- +--------+
- | 李四 |
- +--------+
- 1 row in set (0.00 sec)
(12)列出所有部门号为2和3的职工号、姓名、党员否
- mysql8.0 [SLB]>select 职工号,姓名,政治面貌 from worker where 部门号=103 or 部门号=102;
- +-----------+--------+--------------+
- | 职工号 | 姓名 | 政治面貌 |
- +-----------+--------+--------------+
- | 1003 | 王亮 | 党员 |
- | 1004 | 赵六 | 群众 |
- | 1005 | 钱七 | 党员 |
- | 1006 | 孙八 | 党员 |
- +-----------+--------+--------------+
- 4 rows in set (0.00 sec)
(13)将职工表worker中的职工按出生的先后顺序排序
- mysql8.0 [SLB]>select 姓名 from worker order by 出生日期;
- +--------+
- | 姓名 |
- +--------+
- | 王亮 |
- | 张三 |
- | 钱七 |
- | 赵六 |
- | 孙八 |
- | 李四 |
- +--------+
- 6 rows in set (0.00 sec)
(14)显示工资最高的前3名职工的职工号和姓名
- mysql8.0 [SLB]>select 职工号,姓名 from worker order by 工资 desc limit 3;
- +-----------+--------+
- | 职工号 | 姓名 |
- +-----------+--------+
- | 1003 | 王亮 |
- | 1004 | 赵六 |
- | 1005 | 钱七 |
- +-----------+--------+
- 3 rows in set (0.00 sec)
(15)求出各部门党员的人数
- mysql8.0 [SLB]>select 部门号,count(*) from worker where 政治面貌='党员' group by 部门号;
- +-----------+----------+
- | 部门号 | count(*) |
- +-----------+----------+
- | 102 | 3 |
- +-----------+----------+
- 1 row in set (0.01 sec)
(16)统计各部门的工资和平均工资
- mysql8.0 [SLB]>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)
(17)列出总人数大于4的部门号和总人数
- mysql8.0 [SLB]>select 部门号,count(姓名) as 总人数 from worker group by 部门号 having count(姓名)>=4;
- +-----------+-----------+
- | 部门号 | 总人数 |
- +-----------+-----------+
- | 102 | 4 |
- +-----------+-----------+
- 1 row in set (0.00 sec)
题2
表信息
要求
(1)查询student表的所有记录
- mysql8.0 [SLB1]>select * from student;
- +-----+-----------+------+-------+--------------+--------------------+
- | id | name | sex | birth | department | address |
- +-----+-----------+------+-------+--------------+--------------------+
- | 901 | 张老大 | 男 | 1985 | 计算机系 | 北京市海淀区 |
- | 902 | 张老二 | 男 | 1986 | 中文系 | 北京市昌平区 |
- | 903 | 张三 | 女 | 1990 | 中文系 | 湖南省永州市 |
- | 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 |
- | 905 | 王五 | 女 | 1991 | 英语系 | 福建省厦门市 |
- | 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 |
- +-----+-----------+------+-------+--------------+--------------------+
- 6 rows in set (0.00 sec)
(2)查询student表的第2到4条记录
- mysql8.0 [SLB1]>select * from student limit 1,3;
- +-----+-----------+------+-------+------------+--------------------+
- | id | name | sex | birth | department | address |
- +-----+-----------+------+-------+------------+--------------------+
- | 902 | 张老二 | 男 | 1986 | 中文系 | 北京市昌平区 |
- | 903 | 张三 | 女 | 1990 | 中文系 | 湖南省永州市 |
- | 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 |
- +-----+-----------+------+-------+------------+--------------------+
- 3 rows in set (0.00 sec)
(3)从student表查询所有的学生的学号(id),姓名(name),和院系(department)的信息
- mysql8.0 [SLB1]>select id,name,department from student;
- +-----+-----------+--------------+
- | id | name | department |
- +-----+-----------+--------------+
- | 901 | 张老大 | 计算机系 |
- | 902 | 张老二 | 中文系 |
- | 903 | 张三 | 中文系 |
- | 904 | 李四 | 英语系 |
- | 905 | 王五 | 英语系 |
- | 906 | 王六 | 计算机系 |
- +-----+-----------+--------------+
- 6 rows in set (0.00 sec)
(4)从student表中查询计算机系和英语系的学生的信息
- mysql8.0 [SLB1]>select * from student where department='计算机系' or department='英语系';
- +-----+-----------+------+-------+--------------+--------------------+
- | id | name | sex | birth | department | address |
- +-----+-----------+------+-------+--------------+--------------------+
- | 901 | 张老大 | 男 | 1985 | 计算机系 | 北京市海淀区 |
- | 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 |
- | 905 | 王五 | 女 | 1991 | 英语系 | 福建省厦门市 |
- | 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 |
- +-----+-----------+------+-------+--------------+--------------------+
- 4 rows in set (0.01 sec)
(5)从studnet表中查询年龄18-22岁的学生信息
- mysql8.0 [SLB1]>select * from student where (select year(curdate())-birth) between 18 and 22;
- Empty set (0.01 sec)
- mysql8.0 [SLB1]>select * from student where (select year(curdate())-birth) between 35 and 40;
- +-----+-----------+------+-------+--------------+--------------------+
- | id | name | sex | birth | department | address |
- +-----+-----------+------+-------+--------------+--------------------+
- | 901 | 张老大 | 男 | 1985 | 计算机系 | 北京市海淀区 |
- | 902 | 张老二 | 男 | 1986 | 中文系 | 北京市昌平区 |
- | 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 |
- +-----+-----------+------+-------+--------------+--------------------+
- 3 rows in set (0.00 sec)
(6)从student表中查询每个院系有多少人
- mysql8.0 [SLB1]>select department,count(*) from student group by department;
- +--------------+----------+
- | department | count(*) |
- +--------------+----------+
- | 计算机系 | 2 |
- | 中文系 | 2 |
- | 英语系 | 2 |
- +--------------+----------+
- 3 rows in set (0.00 sec)
(7)从score表中查询每个科目的最高分
- mysql8.0 [SLB1]>select c_name,max(grade) from score group by c_name;
- +-----------+------------+
- | c_name | max(grade) |
- +-----------+------------+
- | 计算机 | 98 |
- | 英语 | 94 |
- | 中文 | 95 |
- +-----------+------------+
- 3 rows in set (0.00 sec)
(8)查询李四的考试科目(c_name)和考试成绩(grade)
- mysql8.0 [SLB1]>select c_name,grade from score as sc,student as st where sc.stu_id=st.id and name='李四';
- +-----------+-------+
- | c_name | grade |
- +-----------+-------+
- | 计算机 | 70 |
- | 英语 | 92 |
- +-----------+-------+
- 2 rows in set (0.00 sec)
(9)用连接的方式查询所有学生的信息和考试信息
- mysql8.0 [SLB1]>select st.id,st.name,st.sex,st.birth,st.department,st.address,sc.id as corid,sc.c_name,sc.grade from student as st inner join score as sc onst.id=sc.stu_id;
- +-----+-----------+------+-------+--------------+--------------------+-------+-----------+-------+
- | id | name | sex | birth | department | address | corid | c_name | grade |
- +-----+-----------+------+-------+--------------+--------------------+-------+-----------+-------+
- | 901 | 张老大 | 男 | 1985 | 计算机系 | 北京市海淀区 | 1 | 计算机 | 98 |
- | 901 | 张老大 | 男 | 1985 | 计算机系 | 北京市海淀区 | 2 | 英语 | 80 |
- | 902 | 张老二 | 男 | 1986 | 中文系 | 北京市昌平区 | 3 | 计算机 | 65 |
- | 902 | 张老二 | 男 | 1986 | 中文系 | 北京市昌平区 | 4 | 中文 | 88 |
- | 903 | 张三 | 女 | 1990 | 中文系 | 湖南省永州市 | 5 | 中文 | 95 |
- | 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 | 6 | 计算机 | 70 |
- | 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 | 7 | 英语 | 92 |
- | 905 | 王五 | 女 | 1991 | 英语系 | 福建省厦门市 | 8 | 英语 | 94 |
- | 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 | 9 | 计算机 | 90 |
- | 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 | 10 | 英语 | 85 |
- +-----+-----------+------+-------+--------------+--------------------+-------+-----------+-------+
- 10 rows in set (0.00 sec)
(10)计算每个学生的总成绩
- mysql8.0 [SLB1]>select st.name,sum(sc.grade) from student as st inner join score as sc on st.id=sc.stu_id group by name;
- +-----------+---------------+
- | name | sum(sc.grade) |
- +-----------+---------------+
- | 张老大 | 178 |
- | 张老二 | 153 |
- | 张三 | 95 |
- | 李四 | 162 |
- | 王五 | 94 |
- | 王六 | 175 |
- +-----------+---------------+
- 6 rows in set (0.00 sec)
(11)计算每个考试科目的平均成绩
- mysql8.0 [SLB1]>select c_name,avg(grade) from score group by c_name;
- +-----------+------------+
- | c_name | avg(grade) |
- +-----------+------------+
- | 计算机 | 80.7500 |
- | 英语 | 87.7500 |
- | 中文 | 91.5000 |
- +-----------+------------+
- 3 rows in set (0.01 sec)
(12)查询计算机成绩低于95的学生信息
- mysql8.0 [SLB1]>select st.id,st.name,st.sex,st.birth,st.department,st.address,sc.c_name,sc.grade from student as st inner join score as sc on st.id=sc.stu_id where sc.c_name='计算机' and sc.grade<95;
- +-----+-----------+------+-------+--------------+--------------------+-----------+-------+
- | id | name | sex | birth | department | address | c_name | grade |
- +-----+-----------+------+-------+--------------+--------------------+-----------+-------+
- | 902 | 张老二 | 男 | 1986 | 中文系 | 北京市昌平区 | 计算机 | 65 |
- | 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 | 计算机 | 70 |
- | 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 | 计算机 | 90 |
- +-----+-----------+------+-------+--------------+--------------------+-----------+-------+
- 3 rows in set (0.00 sec)
(13)查询同时参加计算机考试和英语考试的学生的信息
- mysql8.0 [SLB1]>select st.id,st.name,st.sex,st.birth,st.department,st.address,concat(sc1.c_name,sc2.c_name) as exam from student as st,(select * from score where c_name='计算机') as sc1 inner join (select * from score where c_name='英语') as sc2 on sc1.stu_id=sc2.stu_id where st.id=sc1.stu_id;
- +-----+-----------+------+-------+--------------+--------------------+-----------------+
- | id | name | sex | birth | department | address | exam |
- +-----+-----------+------+-------+--------------+--------------------+-----------------+
- | 901 | 张老大 | 男 | 1985 | 计算机系 | 北京市海淀区 | 计算机英语 |
- | 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 | 计算机英语 |
- | 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 | 计算机英语 |
- +-----+-----------+------+-------+--------------+--------------------+-----------------+
- 3 rows in set (0.00 sec)
(14)将计算机考试成绩按从高到低进行排序
- mysql8.0 [SLB1]>select * from score order by grade desc;
- +----+--------+-----------+-------+
- | id | stu_id | c_name | grade |
- +----+--------+-----------+-------+
- | 1 | 901 | 计算机 | 98 |
- | 5 | 903 | 中文 | 95 |
- | 8 | 905 | 英语 | 94 |
- | 7 | 904 | 英语 | 92 |
- | 9 | 906 | 计算机 | 90 |
- | 4 | 902 | 中文 | 88 |
- | 10 | 906 | 英语 | 85 |
- | 2 | 901 | 英语 | 80 |
- | 6 | 904 | 计算机 | 70 |
- | 3 | 902 | 计算机 | 65 |
- +----+--------+-----------+-------+
- 10 rows in set (0.00 sec)
(15)从student表和score表中查询出学生的学号,然后合并查询结果
- mysql8.0 [SLB1]>select st.id,st.name from student as st inner join score as sc on st.id=sc.stu_id;
- +-----+-----------+
- | id | name |
- +-----+-----------+
- | 901 | 张老大 |
- | 901 | 张老大 |
- | 902 | 张老二 |
- | 902 | 张老二 |
- | 903 | 张三 |
- | 904 | 李四 |
- | 904 | 李四 |
- | 905 | 王五 |
- | 906 | 王六 |
- | 906 | 王六 |
- +-----+-----------+
- 10 rows in set (0.01 sec)
(16)查询姓张或者姓王的同学的姓名、院系和考试科目及成绩
- mysql8.0 [SLB1]>select st.name,st.department,sc.c_name,sc.grade from student as st inner join score as sc on st.id=sc.stu_id where name like '张%' or name likke '王%';
- +-----------+--------------+-----------+-------+
- | name | department | c_name | grade |
- +-----------+--------------+-----------+-------+
- | 张老大 | 计算机系 | 计算机 | 98 |
- | 张老大 | 计算机系 | 英语 | 80 |
- | 张老二 | 中文系 | 计算机 | 65 |
- | 张老二 | 中文系 | 中文 | 88 |
- | 张三 | 中文系 | 中文 | 95 |
- | 王五 | 英语系 | 英语 | 94 |
- | 王六 | 计算机系 | 计算机 | 90 |
- | 王六 | 计算机系 | 英语 | 85 |
- +-----------+--------------+-----------+-------+
- 8 rows in set (0.00 sec)
(17)查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩
- mysql8.0 [SLB1]>select st.name,(year(curdate())-birth) as age,st.address,st.department,sc.c_name,sc.grade from student as st inner join score as sc on st.id=sc.stu_id where address like '%湖南%';
- +--------+------+--------------------+--------------+-----------+-------+
- | name | age | address | department | c_name | grade |
- +--------+------+--------------------+--------------+-----------+-------+
- | 张三 | 33 | 湖南省永州市 | 中文系 | 中文 | 95 |
- | 王六 | 35 | 湖南省衡阳市 | 计算机系 | 计算机 | 90 |
- | 王六 | 35 | 湖南省衡阳市 | 计算机系 | 英语 | 85 |
- +--------+------+--------------------+--------------+-----------+-------+
- 3 rows in set (0.00 sec)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。