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,
`性别` char(10) NOT NULL,
PRIMARY KEY (`职工号`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `性别`, `出生日期`) VALUES (101, 1001, '2015-05-04', 3500.00, '群众', '张三', '男', '1990-07-01');
INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `性别`, `出生日期`) VALUES (101, 1002, '2017-02-06', 3200.00, '团员', '李四', '男', '1997-02-08');
INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `性别`, `出生日期`) VALUES (102, 1003, '2011-01-04', 8500.00, '党员', '王亮', '男', '1983-06-08');
INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `性别`, `出生日期`) VALUES (102, 1004, '2016-10-10', 5500.00, '群众', '赵六', '男', '1994-09-05');
INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `性别`, `出生日期`) VALUES (102, 1005, '2014-04-01', 4800.00, '党员', '钱七', '女', '1992-12-30');
INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `性别`, `出生日期`) VALUES (102, 1006, '2017-05-05', 4500.00, '党员', '孙八', '女', '1996-09-02');
INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `性别`, `出生日期`) VALUES (101, 1007, '2015-05-07', 1500.00, '党员', '陈九', '女', '1958-09-02');
INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `性别`, `出生日期`) VALUES (101, 1008, '2015-05-07', 1800.00, '群众', '刘毅', '男', '1970-09-02');
mysql> select *from worker;
mysql> select 部门号 from worker group by 部门号;
mysql> select count(*) from worker;
mysql> select max(工资),min(工资) from worker;
mysql> select avg(工资),sum(工资) from worker;
myql->create table 工作日期表 (
->职工号 int(11) NOT NULL,
->姓名 varchar(100) NOT NULL, `
->工作时间 int(10) NOT NULL );
mysql> select 姓名, year(current_date()) - year(出生日期) as 年龄 from worker where 性别 = '女';
mysql> select 职工号,姓名,出生日期 from worker where 姓名 like '刘%';
mysql> select 姓名,工作时间 from worker where year(出生日期)<1960;
mysql> select 姓名 from worker where 工资>=1000 and 工资<=2000;
mysql> select 姓名 from worker where 姓名 like'陈%' or 姓名 like'李%';
mysql> select 职工号, 姓名, case when 政治面貌 = '党员' then '是' else '否' end as 党员否 from worker where 部门号 in (101, 102);
mysql> select 姓名,出生日期 from worker order by 出生日期;
mysql> select 职工号,姓名,工资 from worker order by 工资 desc limit 3;
mysql> select 部门号,count(*) as 党员人数 from worker where 政治面貌 = '党员' group by 部门号;
mysql> select 部门号,count(*)总人数 from worker group by 部门号 having count(*)>=4;
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)
);
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,'计算机系', '湖南省衡阳市');
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);
mysql> select *from student;
mysql> select *from student limit 1,3;
mysql> select id,name,department from student;
mysql> select *from student where department in('计算机系','英语系');
mysql> select *from student where year(current_date())-year(birth)>=18 and year(current_date())-year(birth)<=20;
mysql> select department,count(*) as 总人数 from student group by department;
mysql> select c_name,max(grade) from score group by c_name;
mysql> select s.c_name, s.grade from score s join student stu on s.stu_id = stu.id where stu.name = '李四';
mysql> select stu.name,stu.sex,stu.birth,stu.department,stu.address,s.c_name,s.grade from student stu inner join score s on stu.id=s.stu_id;
mysql> select stu.name,sum(s.grade) as 总成绩 from student stu inner join score s on stu.id=s.stu_id group by stu.id,stu.name;
mysql> select stu.name,avg(s.grade) as 平均成绩 from student stu inner join score s on stu.id=s.stu_id group by stu.id,stu.name;
12.查询计算机成绩低于95的学生信息
mysql> select stu.name, stu.sex, stu.birth, stu.department, stu.address, s.c_name, s.grade from student stu inner join score s on stu.id=s.stu_id where s.c_name='计算机' and
s.grade<95;
13.查询同时参加计算机和英语考试的学生的信息
mysql> select stu.name, stu.sex, stu.birth, stu.department, stu.address, s.c_name, s.grade from student stu inner join score s on stu.id=s.stu_id where s.c_name='计算机' or s.c_name='英语';
14.将计算机考试成绩按从高到低进行排序
mysql> select stu.name, stu.sex, stu.birth, stu.department, stu.address, s.c_name, s.grade from student stu inner join score s on stu.id=s.stu_id where s.c_name='计算机' order by s.grade desc;
15.从student表和score表中查询出学生的学号,然后合并查询结果
mysql> select stu_id from score;
mysql> select id from student;
mysql> select stu.id,s.stu_id from student stu inner join score s on stu.id=s.stu_id;
16.查询姓张或者姓王的同学的姓名、院系和考试科目及成绩
mysql> select stu.name, stu.department, s.c_name, s.grade from student stu inner join score s on stu.id = s.stu_id where stu.name like '张%' or stu.name like '王%';
17.查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩
mysql>select stu.name, stu.department, s.c_name, s.grade from student stu inner join score s on stu.id = s.stu_id where stu.address like'湖南%';