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

数据库后续

-- 添加作者字段
alter table t_hero add author varchar(100);
 
-- 更新数据
 
update  t_hero set author = "曹雪芹" where id = 1;
update  t_hero set author = "曹雪芹" where id = 2;
update  t_hero set author = "曹雪芹" where id = 3;
update  t_hero set author = "曹雪芹" where id = 4;
 
update  t_hero set author = "吴承恩" where id = 5;
update  t_hero set author = "吴承恩" where id = 6;
update  t_hero set author = "吴承恩" where id = 7;
update  t_hero set author = "吴承恩" where id = 8;
 
update  t_hero set author = "罗贯中" where id = 9;
update  t_hero set author = "罗贯中" where id = 10;
update  t_hero set author = "罗贯中" where id = 11;
update  t_hero set author = "罗贯中" where id = 12;
update  t_hero set author = "罗贯中" where id = 13;
 
update  t_hero set author = "施耐庵" where id = 14;
update  t_hero set author = "施耐庵" where id = 15;
update  t_hero set author = "施耐庵" where id = 16;
update  t_hero set author = "施耐庵" where id = 17;
update  t_hero set author = "施耐庵" where id = 18;
 
-- 查看t_hero表
select * from t_hero;

 

 -- 根据id删除数据
 
delete from t_hero where id = 18;
 
select * from t_hero;

 

 -- 增加数据
insert into t_hero(name, book, author) VALUES ('武松', '水浒传', '施耐庵');
select * from t_hero;

 

 

单表查询 

素材: 表名: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(*) from worker;

 

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

 select max(`工资`) from worker;
select min(`工资`) from worker;

 

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

select avg(`工资`) from worker;
select sum(`工资`) from worker;

 

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

CREATE TABLE `工作日期表` (
         `职工号` int(11) NOT NULL,
         `姓名` varchar(20) NOT NULL,
         `出生日期` date NOT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;

 7、显示所有党员的名字。 

select `姓名` from worker where `政治面貌` = '党员';

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

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

 

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

 select `姓名`, `工作时间` from worker where `出生日期` < 1960-00-00;

 

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

select `姓名` from worker where "工资" > 2000 and "工资" < 3000;

 

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

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

 

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

select `职工号`, `姓名`, `政治面貌` from worker where `部门号` = 102 or `部门号` = 103; 

 

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

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

 

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

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

 

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

select count(*) as 党员人数 from worker where 政治面貌="党员";

 

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

select `工资`,  (select  avg(`工资`) from worker where `部门号` = 101) as 平均工资  from worker where `部门号` = 101;
select `工资`,  (select  avg(`工资`) from worker where `部门号` = 102) as 平均工资  from worker where `部门号` = 102;

 

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

select `部门号`, count(*) as 总人数 from worker group by `部门号` having 总人数 > 4;

 

多表查询 

1.创建student和score表

-- 创建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)
);
 
 
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 where id = 902 or id = 904;

 

 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 (2025 - birth) between 2007 and 2003;

 

 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 name, c_name, grade from score,student where stu_id = 904 and student.id = stu_id;

 

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

select * from student, score where student.id = stu_id;

 

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

SELECT
         student.id AS 学生ID,
         student.name AS 姓名,
         SUM(score.grade) AS 总成绩
     FROM
         student
     INNER JOIN
         score ON student.id = score.stu_id
     GROUP BY
         student.id, student.name;

 

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

select c_name, avg(grade) from score group by c_name;

 

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

select * from student,score where student.id = stu_id and  grade < 95 and c_name = "计算机";

 

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

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

 

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

select c_name,grade from score  where c_name="计算机" order by  grade desc;
 

 

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

SELECT id AS 学号 FROM student
UNION
SELECT stu_id AS 学号 FROM score;

 

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

SELECT
         s.name AS 姓名,
         s.department AS 院系,
         sc.c_name AS 考试科目,
         sc.grade AS 成绩
     FROM
         student s
     JOIN
         score sc ON s.id = sc.stu_id
     WHERE
         s.name LIKE '张%' OR s.name LIKE '王%';

 

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

select name, birth, department, c_name, grade from student join score on student.id = score.stu_id where address like "湖南%";

 

相关文章:

  • python实现登录页面图形验证码
  • Pydantic字段元数据指南:从基础到企业级文档增强
  • 【软考备考】系统架构设计论文完整范文示例
  • iOS自定义collection view的page size(width/height)分页效果
  • 横扫SQL面试——事件流处理(峰值统计)问题
  • 8.3MW屋顶光伏+光储协同:上海汽车变速器低碳工厂的能源革命-安科瑞黄安南
  • 飞桨PP系列新成员PP-DocLayout开源,版面检测加速大模型数据构建,超百页文档图像一秒搞定
  • 解决 “Cannot read SQL script from class path resource [sql/XX.sql]“ 错误
  • 每日总结3.28
  • 卷积神经网络 - 转置卷积
  • Neo4j GDS-05-neo4j GDS 库中对应的中心性分析算法介绍
  • Netty——零拷贝
  • 上海SMT贴片技术解析与行业趋势
  • 【CSS3】02-选择器 + CSS特性 + 背景属性 + 显示模式
  • axios文件下载使用后端传递的名称
  • PyQt6实例_批量下载pdf工具_exe使用方法
  • OSPF邻居状态机
  • MAC环境给docker换源
  • 硬件老化测试方案的设计误区
  • open-cv的安装
  • 国家发改委:正在会同有关方面,加快构建统一规范、协同共享、科学高效的信用修复制度
  • 新华社原香港分社副社长、深圳市委原副书记秦文俊逝世
  • 世卫大会拒绝涉台提案,外交部:坚持一个中国原则是人心所向
  • 巴基斯坦副总理兼外长达尔将访华
  • 上海银行副行长汪明履新上海农商银行党委副书记
  • 江南考古文脉探寻