一、单表查询
创建表worker并插入数据
表名: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, `性别` enum('男','女'), `出生日期` 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');
-- 查询所有数据验证 select * from worker;
数据库表的查询
-- 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 * from worker where `性别` = '女'; -- 8、列出所有姓刘的职工信息 select `职工号`, `姓名`, `出生日期` from worker where `姓名` like '刘%'; -- 9、列出1960年以前出生的职工 select `姓名`, `工作时间` from worker where `出生日期` < '1960-01-01'; -- 10、列出工资在1000-2000之间的职工 select `姓名` from worker where `工资` between 1000 and 2000; -- 11、列出姓陈或李的职工 select `姓名` from worker where `姓名` like '陈%' or `姓名` like '李%'; -- 12、列出部门号为2和3的职工信息 select `职工号`, `姓名`, if(`政治面貌` = '党员', '是', '否') as `党员否` from worker where `部门号` in (102, 103); -- 根据实际部门号调整 -- 13、按出生日期排序 select * from worker order by `出生日期`; -- 14、显示工资前三名的职工 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 `部门号`, count(*) as `总人数` from worker group by `部门号` having count(*) > 4;
查询结果
11.
12.
13.
14.
15,16,17
二、多表查询
创建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) );
插入数据
向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 1, 3; -- 5. 查询所有学生的学号(id)、姓名(name)和院系(department)的信息 select id, name, department from student; -- 6. 查询计算机系和英语系的学生的信息 select * from student where department in ('计算机系', '英语系'); -- 7. 查询年龄 18~22 岁的学生信息 select * from student where (2023 - birth) between 18 and 22; -- 8. 查询每个院系有多少人 select department, count(*) as 人数 from student group by department; -- 9. 查询每个科目的最高分 select c_name, max(grade) as 最高分 from score group by c_name; -- 10. 查询李四的考试科目和考试成绩 select c_name, grade from score where stu_id = (select id from student where name = '李四'); -- 11. 用连接的方式查询所有学生的信息和考试信息 select student.*, score.c_name, score.grade from student join score on student.id = score.stu_id; -- 12. 计算每个学生的总成绩 select stu_id, sum(grade) as 总成绩 from score group by stu_id; -- 13. 计算每个考试科目的平均成绩 select c_name, avg(grade) as 平均成绩 from score group by c_name; -- 14. 查询计算机成绩低于 95 的学生信息 select student.* from student join score on student.id = score.stu_id where score.c_name = '计算机' and score.grade < 95; -- 15. 查询同时参加计算机和英语考试的学生的信息 select student.* from student where id in (select stu_id from score where c_name = '计算机') and 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 student.name, student.department, score.c_name, score.grade from student join score on student.id = score.stu_id where student.name like
注意:sql语句中不区分大小写,这里为了方便书写所以使用了select * ,但在真实数据库表查询中一般不建议直接使用select * ,最好使用select后面跟字段的方法查询
总结
到此这篇关于mysql数据库中表的查询的文章就介绍到这了,更多相关mysql表的查询内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论