当前位置: 首页 > news >正文

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、显示所有职工的基本信息。

select * from worker;

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

select distinct 部门号 from worker;

3、求出所有职工的人数。

select count(*) as 职工总数 from worker;

4、列出最高工和最低工资。

select max(工资) as 最高工资, min(工资) as 最低工资 from worker;

5、列出职工的平均工资和总工资。

select avg(工资) as 平均工资, sum(工资) as 总工资 from worker;

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

CREATE TABLE 工作日期表 AS
SELECT 职工号, 姓名, 工作时间 FROM worker;

7、显示所有职工的年龄。

select `姓名`, timestampdiff(year,`出生日期`, curdate()) as 年龄 from worker;

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

select 职工号,姓名,出生日期 from worker where 姓名 like '赵%';

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

SELECT 姓名, 工作时间 FROM worker WHERE 出生日期 < '1960-01-01';

10、列出工资在3000-5000之间的所有职工姓名。

select 姓名 from worker where 工资 between 3000 and 5000;

11、列出所有陈姓和李姓的职工姓名。

select 姓名 from worker where 姓名 like '陈%' or 姓名 like '李%';

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

select 职工号, 姓名, case when 政治面貌 = '党员' then '是' else '否' end as 党员否 from worker where 部门号 in (101, 102);

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

select * from worker order by 出生日期 asc;

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

select 职工号,姓名 from worker order by 工资 desc limit 3;

15、求出各部门党员的人数。

select 部门号, count(*) as 党员人数 from worker where 政治面貌 = '党员' group by 部门号;

16、统计各部门的工资和平均工资

select 部门号, sum(工资) as 总工资, avg(工资) as 平均工资 from worker group by 部门号;

17、列出总人数大于4的部门号和总人数。

select 部门号 from worker group by 部门号 having count(*) >= 4;

二、多表查询

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)
);

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,'计算机系', '湖南省衡阳市');

向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);

3.查询student表的所有记录

select * from student;

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

select * from student limit 3 offset 1;

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

select id, name, department from student;

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

select * from student where department = '计算机系' or department = '英语系';

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

select * from student where (year(curdate()) - birth) between 18 and 20;

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

select department, count(*) as 人数 from student group by department;

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

select c_name, max(grade) as 最高分 from score group by c_name;

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

select c_name, grade from score join student on score.stu_id = student.id where student.name='李四'

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

select st.*, s.c_name, s.grade from student st left join score s on st.id = s.stu_id;

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

select stu_id, sum(grade) as 总成绩 from score group by stu_id;

select st.id, st.name, sum(s.grade) as 总成绩 from student st left score s on st.id = s.stu_id group by st.id;

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

select c_name, avg(grade) as 平均成绩 from score group by c_name;

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

select st.* from student st join score s on st.id = s.stu_id where s.c_name='计算机' and s.grade < 95;

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

select st.* from student st where st.id in (select stu_id from score where c_name = '计算机') and st.id in (select stu_id from score where c_name='英语');

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

select * from score where c_name='计算机' order by grade desc;

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

select id from student union select stu_id from score;

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

select st.name, st.department, s.grade from student st join score s on st.id = s.stu_id where name like '张%' or name like '王%';

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

select st.name, (year(curdate()) - st.birth) as 年龄, st.department, s.c_name, s.grade from student st join score s on st.id = s.stu_id where address like '湖南%';
http://www.dtcms.com/a/99834.html

相关文章:

  • linux实现rsync+sersync实时数据备份
  • Redis-快速入门
  • GPT Actions
  • 【硬件测试】基于FPGA的16QAM+帧同步系统开发与硬件片内测试,包含高斯信道,误码统计,可设置SNR
  • 【Find My全球市场观察:中国制造如何改写游戏规则?】
  • 游戏引擎学习第191天
  • 【Python】工作笔记:返回当月第一天、昨天;上月第一天、当天;全年节假日
  • 淘宝客户端动态化页面搭建
  • Linux课程学习一
  • 【区块链安全 | 第五篇】DeFi概念详解
  • Kubernetes比同规格虚拟机性能相差多少?
  • 《Express:Node.js 里的 “闪电侠”》
  • Playwright从入门到实战:比Selenium更快的数据爬取案例实战
  • STL-string容器
  • 蓝桥杯嵌入式学习笔记
  • SQL IF(xxx, 1, 0) 窗口函数
  • 【嵌入式学习3】TCP服务器客户端 - UDP发送端接收端
  • Supabase 匿名密钥与服务角色密钥详细对比文档
  • .NET 9 中的新增功能:关键更新和 C# 12 功能简化
  • C#核心学习(一)面向过程与面向对象编程---初识类和对象
  • 深入理解MySQL聚集索引与非聚集索引
  • fetch的语法规则及常见用法
  • EasyExcel 与 Apache POI:Java 操作 Excel 的详解
  • 6-1-1 利用AI完成一个可视化看板
  • 如何监控和优化服务器的 CPU 性能
  • 视频联网平台智慧运维系统:智能时代的城市视觉中枢
  • 记录一次Dell服务器更换内存条报错解决过程No memory found
  • 基于微波光子信道的图像传输系统matlab仿真,调制方式采用OFDM+QPSK,LDPC编译码以及LS信道估计
  • 机器学习——集成学习框架(GBDT、XGBoost、LightGBM、CatBoost)、调参方法
  • 计算机基础