use test; create table t1(tid smallint(5) unsigned auto_increment,tname varchar(50),tkecheng varchar(50),tgrade smallint(10),primary key(tid))ENGINE=i ...
use test;
create table t1(
tid smallint(5) unsigned auto_increment,
tname varchar(50),
tkecheng varchar(50),
tgrade smallint(10),
primary key(tid)
)ENGINE=innodb default charset=utf8;
INSERT into t1(tname,tkecheng,tgrade)
values('xm','yuwen',76),
('xm','shuxue',89),
('xm1','yuwen',73),
('xm1','shuxue',84),
('xm2','yuwen',72),
('xm2','shuxue',88),
('xm3','yuwen',70),
('xm3','shuxue',69),
('xm4','yuwen',69),
('xm4','shuxue',79),
('xm5','yuwen',76),
('xm5','shuxue',78),
('xm6','yuwen',84),
('xm6','shuxue',83),
('xm7','yuwen',76),
('xm7','shuxue',68),
('xm8','yuwen',84),
('xm8','shuxue',78),
('xm9','yuwen',76),
('xm9','shuxue',98),
('xm10','yuwen',84),
('xm10','shuxue',78),
('xm11','yuwen',86),
('xm11','shuxue',68);
commit;
select * from t1;
#1 數學成績最高的前10條記錄,按數學成績降序排列
select * from t1 where t1.tkecheng='shuxue' order by t1.tgrade desc limit 10;
#2 查詢所有科目都在80分以上記錄
select * from t1 where t1.tgrade>=80;
s(sno,sn,sd,sa)sno,sn,sd,sa分別代表學號,學員姓名,班級,學員年齡
c(cno,cn)cno,cn分別代表課程編號,課程名稱
sc(sno,cno,g)sno,cno,g分別代表學號,所選的課程編號,學習成績
create table s(
sno smallint(5),
sn varchar(50),
sd varchar(50),
sa smallint(10)
)ENGINE=innodb default charset=utf8;
insert into s(sno,sn,sd,sa)
values(1001,'小明','班級1',24),
(1002,'小趙','班級1',22),
(1003,'小黃','班級2',23),
(1004,'小張','班級2',25),
(1005,'小李','班級3',23),
(1006,'小羅','班級3',22),
(1007,'小增','班級3',25);
commit;
create table c(
cno smallint(5),
cn varchar(50)
)ENGINE=innodb default charset=utf8;
insert into c(cno,cn)
values(9001,'功能測試'),
(9002,'自動化測試'),
(9003,'性能測試'),
(9004,'安全性測試'),
(9005,'體驗性測試'),
(9006,'全棧測試');
commit;
create table sc(
sno smallint(5),
cno varchar(50),
g varchar(50)
)ENGINE=innodb default charset=utf8;
insert into sc(sno,cno,g)
values(1001,9001,78),
(1001,9002,68),
(1001,9003,84),
(1001,9004,62),
(1001,9005,98),
(1001,9006,89),
(1002,9001,68),
(1002,9002,64),
(1002,9003,83),
(1002,9004,63),
(1002,9005,90),
(1003,9003,68),
(1004,9004,99),
(1005,9005,99),
(1006,9001,69),
(1006,9002,67),
(1006,9003,86),
(1006,9004,87),
(1006,9005,90),
(1006,9006,91),
(1007,9001,99);
commit;
1)查詢選修課程名稱為‘性能測試’的學員學號和姓名?
select s.sno,s.sn from s,sc,c
where s.sno=sc.sno
and sc.cno=c.cno
and c.cn='性能測試';
2)查詢選修課程超過5門的學員學號和所屬班級?
select s.sno,s.sd from s
where s.sno
in (
select sc.sno
from c,sc
where c.cno=sc.cno
group by sc.sno
having count(*)>5
);
3)用戶名為xuelei在2013-09-16和2013-09-17天插入的任務。
任務名包含"Arrow",文件大小大於200
按文件由大到小排序,只提取出前5條數據
create table User_task(
Username varchar(50) comment '用戶名',
Taskid int comment '任務id',
Taskname varchar(50) comment '任務名',
Task_filesize int comment '文件大小',
Insert_time datetime comment '插入日期'
)ENGINE=innodb default charset=utf8;
insert into User_task(Username,Taskid,Taskname,Task_filesize,Insert_time)
values('xuelei',1001,'Arrow is not a ',200,str_to_date('2013-09-15 09:00:00', '%Y-%m-%d %H:%i:%S')),
('xuelei',1002,'hao Arrow is not b ',201,str_to_date('2013-09-16 09:00:01', '%Y-%m-%d %H:%i:%S')),
('xuelei',1003,'cc Arrow is not c ',203,str_to_date('2013-09-17 09:01:00', '%Y-%m-%d %H:%i:%S')),
('xuelei',1004,'ddArrow is not d ',204,str_to_date('2013-09-18 09:00:02', '%Y-%m-%d %H:%i:%S')),
('xuelei',1005,'efArrow is not e ',203,str_to_date('2013-09-19 09:20:00', '%Y-%m-%d %H:%i:%S')),
('ceshi',2001,'agbrrow is not a ',203,str_to_date('2013-09-16 09:10:00', '%Y-%m-%d %H:%i:%S')),
('ceshi',2002,'brrow is not b ',203,str_to_date('2013-09-16 16:20:00', '%Y-%m-%d %H:%i:%S')),
('ceshi',2003,'brrow is not c ',203,str_to_date('2013-09-15 09:20:00', '%Y-%m-%d %H:%i:%S')),
('xuelei',2002,'Afrrow is not b ',303,str_to_date('2013-09-16 16:20:00', '%Y-%m-%d %H:%i:%S')),
('xuelei',2003,'arrow is not c ',303,str_to_date('2013-09-16 09:20:00', '%Y-%m-%d %H:%i:%S')),
('xuelei',3001,'Acrrow is not a ',200,str_to_date('2013-09-15 09:00:00', '%Y-%m-%d %H:%i:%S')),
('xuelei',3002,'AArrow is not b ',201,str_to_date('2013-09-16 09:00:01', '%Y-%m-%d %H:%i:%S')),
('xuelei',3003,'aaArrow is not c ',203,str_to_date('2013-09-17 09:01:00', '%Y-%m-%d %H:%i:%S')),
('xuelei',3004,'Arrow is not d ',204,str_to_date('2013-09-18 09:00:02', '%Y-%m-%d %H:%i:%S')),
('xuelei',3005,'Acrrow is not e ',203,str_to_date('2013-09-19 09:20:00', '%Y-%m-%d %H:%i:%S')),
('ceshi',4001,'brrow is not a ',203,str_to_date('2013-09-20 09:10:00', '%Y-%m-%d %H:%i:%S')),
('ceshi',4003,'brrow is not c ',203,str_to_date('2013-09-22 09:20:00', '%Y-%m-%d %H:%i:%S')),
('xuelei',5002,'Arrow is not b ',303,str_to_date('2013-09-23 16:20:00', '%Y-%m-%d %H:%i:%S')),
('xuelei',5003,'arrow is not c ',303,str_to_date('2013-09-24 09:20:00', '%Y-%m-%d %H:%i:%S')),
('xuelei',1011,'Arrow is not c ',201,str_to_date('2013-09-19 09:00:00', '%Y-%m-%d %H:%i:%S')),
('xuelei',1008,'arrow is not a ',199,str_to_date('2013-09-15 09:00:00', '%Y-%m-%d %H:%i:%S')),
('xuelei',1009,'arrow is not b ',200,str_to_date('2013-09-15 09:00:00', '%Y-%m-%d %H:%i:%S')),
('xuelei',1010,'arrow is not c ',201,str_to_date('2013-09-15 09:00:00', '%Y-%m-%d %H:%i:%S')),
('xuelei',1012,'Arrow is not b ',201,str_to_date('2013-09-15 23:59:59', '%Y-%m-%d %H:%i:%S')),
('xuelei',1013,'Arrow is not c ',201,str_to_date('2013-09-17 23:59:59', '%Y-%m-%d %H:%i:%S'));-
('xuelei',1014,'what is a Arrow is not c ',202,str_to_date('2013-09-17 23:59:59', '%Y-%m-%d %H:%i:%S'));
('xuelei',1015,' Arrow Arrow Arrow',203,str_to_date('2013-09-17 23:59:59', '%Y-%m-%d %H:%i:%S'));
commit;
select * from User_task ut
where ut.username='xuelei'
and INSTR(ut.Taskname,binary 'Arrow')<>0
and ut.task_filesize>200
and date_format(ut.insert_time,'%Y-%m-%d') between '2013-09-16' and '2013-09-17'
order by ut.task_filesize
desc limit 5;