準備數據的sql代碼:sql
create table score( id int primary key auto_increment, name varchar(20), subject varchar(20), score int); insert into score values (null,'張三','語文',81), (null,'張三','數學',75), (null,'李四','語文',76), (null,'李四','數學',90), (null,'王五','語文',81), (null,'王五','數學',100), (null,'王五 ','英語',90);
答案:數據庫
select distinct name from score where name not in (select distinct name from score where score<=80)
請用SQL語句實現:從TestDB數據表中查詢出全部月份的發生額都比101科目相應月份的發生額高的科目。請注意:TestDB中有不少科目,都有1-12月份的發生額。 AccID:科目代碼,Occmonth:發生額月份,DebitOccur:發生額。 數據庫名:JcyAudit,數據集:Select * from TestDB函數
準備數據的sql代碼: drop table if exists TestDB; create table TestDB( id int primary key auto_increment, AccID varchar(20), Occmonth date, DebitOccur bigint); insert into TestDB values (null,'101','1988-1-1',100), (null,'101','1988-2-1',110), (null,'101','1988-3-1',120), (null,'101','1988-4-1',100), (null,'101','1988-5-1',100), (null,'101','1988-6-1',100), (null,'101','1988-7-1',100), (null,'101','1988-8-1',100); --複製上面的數據,故意把第一個月份的發生額數字改小一點 insert into TestDB values (null,'102','1988-1-1',90), (null,'102','1988-2-1',110), (null,'102','1988-3-1',120), (null,'102','1988-4-1',100), (null,'102','1988-5-1',100), (null,'102','1988-6-1',100), (null,'102','1988-7-1',100), (null,'102','1988-8-1',100); --複製最上面的數據,故意把全部發生額數字改大一點 insert into TestDB values (null,'103','1988-1-1',150), (null,'103','1988-2-1',160), (null,'103','1988-3-1',180), (null,'103','1988-4-1',120), (null,'103','1988-5-1',120), (null,'103','1988-6-1',120), (null,'103','1988-7-1',120), (null,'103','1988-8-1',120); --複製最上面的數據,故意把全部發生額數字改大一點 insert into TestDB values (null,'104','1988-1-1',130), (null,'104','1988-2-1',130), (null,'104','1988-3-1',140), (null,'104','1988-4-1',150), (null,'104','1988-5-1',160), (null,'104','1988-6-1',170), (null,'104','1988-7-1',180), (null,'104','1988-8-1',140); --複製最上面的數據,故意把第二個月份的發生額數字改小一點 insert into TestDB values (null,'105','1988-1-1',100), (null,'105','1988-2-1',80), (null,'105','1988-3-1',120), (null,'105','1988-4-1',100), (null,'105','1988-5-1',100), (null,'105','1988-6-1',100), (null,'105','1988-7-1',100), (null,'105','1988-8-1',100);
答案:post
select distinct AccID from TestDB where AccID not in (select TestDB.AccIDfrom TestDB, (select * from TestDB where AccID='101') as db101 where TestDB.Occmonth=db101.Occmonth and TestDB.DebitOccur<=db101.DebitOccur );
year month amount 1991 1 1.1 1991 2 1.2 1991 3 1.3 1991 4 1.4 1992 1 2.1 1992 2 2.2 1992 3 2.3 1992 4 2.4 查成這樣一個結果: year m1 m2 m3 m4 1991 1.1 1.2 1.3 1.4 1992 2.1 2.2 2.3 2.4
準備sql語句: drop table if exists sales; create table sales( id int auto_increment primary key, year varchar(10), month varchar(10), amount float(2,1)); insert into sales values (null,'1991','1',1.1), (null,'1991','2',1.2), (null,'1991','3',1.3), (null,'1991','4',1.4), (null,'1992','1',2.1), (null,'1992','2',2.2), (null,'1992','3',2.3), (null,'1992','4',2.4);
答案:學習
select sales.year, (select t.amount from sales as t where t.month='1' and t.year = sales.year) as 'm1', (select t.amount from sales as t where t.month='2' and t.year = sales.year) as 'm2', (select t.amount from sales as t where t.month='3' and t.year = sales.year) as 'm3', (select t.amount from sales as t where t.month='4' and t.year = sales.year) as 'm4' from sales group by year
表:id,title,postuser,postdate,parentidthis
準備sql語句: drop table if exists articles; create table articles( id int auto_increment primary key, title varchar(50), postuser varchar(10), postdate datetime, parentid int references articles(id) ); insert into articles values (null,'第一條','張三','1998-10-10 12:32:32',null), (null,'第二條','張三','1998-10-10 12:34:32',null), (null,'第一條回覆1','李四','1998-10-10 12:35:32',1), (null,'第二條回覆1','李四','1998-10-10 12:36:32',2), (null,'第一條回覆2','王五','1998-10-10 12:37:32',1), (null,'第一條回覆3','李四','1998-10-10 12:38:32',1), (null,'第二條回覆2','李四','1998-10-10 12:39:32',2), (null,'第一條回覆4','王五','1998-10-10 12:39:40',1);
答案:spa
select zhu.title,zhu.postuser, (select max(fu.postdate) from articles as fu where zhu.id = fu.parentid) as postdata from articles as zhu where zhu.parentid is null
學生表 以下: id號 學號 姓名 課程編號 課程名稱 分數 1 2005001 張三 0001 數學 69 2 2005002 李四 0001 數學 89 3 2005001 張三 0001 數學 69
準備數據: create table student2( id int auto_increment primary key, code varchar(20), name varchar(20) ); insert into student2 values (null,'2005001','張三'), (null,'2005002','李四'), (null,'2005001','張三');
答案:code
delete from student2 where id not in(select min(id) from (select * from student2) as t group by t.name);
實驗環境: create table city( cityID int auto_increment primary key, cityName varchar(20)); create table flight ( flightID int auto_increment primary key, StartCityID int references city(cityID), endCityID int references city(cityID), StartTime timestamp); //航班原本應該沒有日期部分纔好,可是下面的題目當中涉及到了日期 insert into city values (null,'北京'), (null,'上海'), (null,'廣州'); insert into flight values (null,1,2,'9:37:23'), (null,1,3,'9:37:23'), (null,1,2,'10:37:23'), (null,2,3,'10:37:23');
(1) 查詢起飛城市是北京的全部航班,按到達城市的名字排序blog
答案:排序
select * from flight as f,city as c where f.endCityID = c.cityID and StartCityID = (select cityID from city where cityName = "北京") order by endCityID desc
(2)查詢北京到上海的全部航班紀錄(起飛城市,到達城市,起飛時間,航班號)
答案:
select c1.cityName as '起飛城市',c2.cityName as '到大城市',f.StartTime as '到大城市',f.flightID as '航班號' from city as c1,city as c2,flight f where c1.cityID = f.StartCityID and c2.cityID = f.endCityID and c1.cityName = "北京" and c2.cityName = "上海"
(3)查詢具體某一天(2005-5-8)的北京到上海的的航班次數
答案:
select count(*) from city as c1,city as c2,flight as f where c1.cityID = f.StartCityID and c2.cityID = f.endCityID and c1.cityName = "北京" and c2.cityName = "上海" and 查幫助得到的某個日期處理函數(startTime) like '2005-5-8%'
準備數據: Drop table if not exists employees; create table employees( id int primary key auto_increment, name varchar(50), salary int, managerid int references employees(id)); insert into employees values (null,' lhm',10000,null), (null,' zxx',15000,1), (null,'flx',9000,1), (null,'tg',10000,2), (null,'wzg',10000,3); Wzg大於flx,lhm大於zxx
答案:
select e.* from employees as m,employees as e where m.id = e.managerid and m.salary < e.salary
實驗數據: drop table if exists tea_stu; drop table if exists teacher; drop table if exists student; create table teacher( teaID int primary key, name varchar(50), age int); create table student( stuID int primary key, name varchar(50), age int); create table tea_stu( teaID int references teacher(teaID), stuID int references student(stuID)); insert into teacher values (1,'zxx',45), (2,'lhm',25) , (3,'wzg',26) , (4,'tg',27); insert into student values (1,'wy',11), (2,'dh',25) , (3,'ysq',26) , (4,'mxc',27); insert into tea_stu values (1,1), (1,2), (1,3); insert into tea_stu values (2,2), (2,3), (2,4); insert into tea_stu values (3,3), (3,4), (3,1); insert into tea_stu values (4,4), (4,1), (4,2), (4,3);
答案:
select teacher.name,count(student.name) as count from teacher,student,tea_stu where teacher.teaID = tea_stu.teaID and student.stuID = tea_stu.stuID and teacher.age < 45 and student.age > 12 group by teacher.name
alter table drop column score; alter table add colunm score int;
可能會很快,可是須要試驗,試驗不能拿真實的環境來操刀,而且要注意,
這樣的操做時沒法回滾的,在個人印象中,只有inert update delete等DML語句才能回滾,
對於create table,drop table ,alter table等DDL語句是不能回滾。
解決方案一, update user set score=0; 解決方案二,假設上面的代碼要執行好長時間,超出咱們的容忍範圍,那我就
alter table user drop column score;alter table user add column score int。
下面代碼實現每一年的那個凌晨時刻進行清零
Runnable runnable = new Runnable(){ public void run(){ clearDb(); schedule(this,new Date(new Date().getYear()+1,0,0)); } }; schedule(runnable,new Date(new Date().getYear()+1,0,1));
name course grade 張三 語文 81 張三 數學 75 李四 語文 76 李四 數學 90 王五 語文 81 王五 數學 100 王五 英語 90
答案:
select name from table group by name having min(grade) > 80
自動編號 學號 姓名 課程編號 課程名稱 分數 1 2005001 張三 0001 數學 69 2 2005002 李四 0001 數學 89 3 2005001 張三 0001 數學 69
刪除除了自動編號不一樣, 其餘都相同的學生冗餘信息
答案:
select 自動編號 from 表 where 自動編號 not in (select min(自動編號) from 表 group by 學號,姓名,課程編號,課程名稱,分數)
select a.name as '主隊',b.name as '客隊' from team as a,team as b where a.name < b.name
實驗數據:
學生表: create table Student( Sid varchar(6), Sname varchar(10), Sage datetime, Ssex varchar(10)); insert into Student values('01' , '趙雷' , '1990-01-01' , '男'); insert into Student values('02' , '錢電' , '1990-12-21' , '男'); insert into Student values('03' , '孫風' , '1990-05-20' , '男'); insert into Student values('04' , '李雲' , '1990-08-06' , '男'); insert into Student values('05' , '周梅' , '1991-12-01' , '女'); insert into Student values('06' , '吳蘭' , '1992-03-01' , '女'); insert into Student values('07' , '鄭竹' , '1989-07-01' , '女'); insert into Student values('08' , '王菊' , '1990-01-20' , '女')
成績表 create table SC( Sid varchar(10), Cid varchar(10), score decimal(18,1)); insert into SC values('01' , '01' , 80); insert into SC values('01' , '02' , 90); insert into SC values('01' , '03' , 99); insert into SC values('02' , '01' , 70); insert into SC values('02' , '02' , 60); insert into SC values('02' , '03' , 80); insert into SC values('03' , '01' , 80); insert into SC values('03' , '02' , 80); insert into SC values('03' , '03' , 80); insert into SC values('04' , '01' , 50); insert into SC values('04' , '02' , 30); insert into SC values('04' , '03' , 20); insert into SC values('05' , '01' , 76); insert into SC values('05' , '02' , 87); insert into SC values('06' , '01' , 31); insert into SC values('06' , '03' , 34); insert into SC values('07' , '02' , 89); insert into SC values('07' , '03' , 98)
課程表 create table Course( Cid varchar(10), Cname varchar(10), Tid varchar(10)); insert into Course values ('01' , '語文' , '02'); insert into Course values ('02' , '數學' , '01'); insert into Course values ('03' , '英語' , '03')
教師表 create table Teacher( Tid varchar(10), Tname varchar(10)); insert into Teacher values ('01' , '張三'); insert into Teacher values ('02' , '李四'); insert into Teacher values ('03' , '王五')
答案:
select s.Sid,s.Sname,avg(score) as a from Student as s,SC where s.Sid = SC.Sid group by s.Sid having a>60
答案:
select s.* from Student as s,SC where s.Sid = SC.Sid group by Sid
答案:
select s.Sid,s.Sname,count(SC.Cid),sum(score) from Student as s left join SC on s.Sid = SC.Sid group by s.Sid
答案:
select s.Sid,S.Sname,count(score),sum(score), sum(case when SC.Cid=01 then score else null end) as score_1, sum(case when SC.Cid=02 then score else null end) as score_2, sum(case when SC.Cid=03 then score else null end) as score_3 from Student as s,SC,Course where s.Sid = SC.Sid and SC.Cid = Course.Cid group by s.Sid
答案:
select count(*) from Teacher where Tname like '李%'
select s.* from Student as s,SC,course,Teacher where s.Sid=SC.Sid and Course.Cid=SC.Cid and Teacher.Tid=Course.Tid and Teacher.Tname = '張三' group by s.Sid
select * from Student where Sid in (select Sid from SC group by Sid having count(cid) < 3)
select s.* from SC,Student as s where Cid in (select Cid from SC where Sid = '01') and s.Sid = SC.Sid group by Sid
select Sname from Student where Sname not in (select s.Sname from Teacher as t,SC,Course,Student as s where t.Tid = Course.Tid and s.Sid=SC.Sid and SC.Cid=Course.Cid and t.Tname = '張三')
select s.Sid,s.Sname,avg(score) from Student as s,SC where s.Sid = SC.Sid and SC.score < 60 group by s.Sid having count(score > 2)
select s.*,SC.score from Student as s,SC where Cid = '01' and s.Sid=SC.Sid and score < 60 group by score desc
select Sid, sum(case when Cid=01 then score else null end) as score_01, sum(case when Cid=02 then score else null end) as score_02, sum(case when Cid=03 then score else null end) as score_03, avg(score) from SC group by Sid order by avg(score) desc
select c.cid as 課程號, c.cname as 課程名稱, count(*) as 選修人數, max(score) as 最高分, min(score) as 最低分, avg(score) as 平均分, sum(case when score >= 60 then 1 else 0 end)/count(*) as 及格率, sum(case when score >= 70 and score < 80 then 1 else 0 end)/count(*) as 中等率, sum(case when score >= 80 and score < 90 then 1 else 0 end)/count(*) as 優良率, sum(case when score >= 90 then 1 else 0 end)/count(*) as 優秀率 from sc, course c where c.cid = sc.cid group by c.cid order by count(*) desc, c.cid asc
select c.Cid as 課程編號, c.Cname as 課程名稱, A.* from course as c, (select Cid, sum(case when score >= 85 then 1 else 0 end)/count(*) as 100_85, sum(case when score >= 70 and score < 85 then 1 else 0 end)/count(*) as 85_70, sum(case when score >= 60 and score < 70 then 1 else 0 end)/count(*) as 70_60, sum(case when score < 60 then 1 else 0 end)/count(*) as 60_0 from SC group by Cid) as A where c.Cid = A.Cid
select s.Sid,s.Sname,count(sc.Cid) from Student as s,SC as sc where s.Sid = sc.Sid group by s.Sid having count(sc.Cid) = 2
select Sname from Student where Sname like '%風%'
select * from Student where Sage like('1990%')
select s.* from Student as s,Teacher as t,SC as sc,Course as c where s.Sid=sc.Sid and c.Tid=t.Tid and sc.Cid = c.Cid and t.Tname='張三' order by sc.score desc limit 0,1
select s.* from Student as s,SC where SC.Sid=s.Sid and SC.Cid = (select c.Cid from Teacher as t,Course as c where t.Tid = c.Tid and t.Tname = '張三') and SC.score = ( select sc.score from Student as s,Teacher as t,SC as sc,Course as c where s.Sid=sc.Sid and c.Tid=t.Tid and sc.Cid = c.Cid and t.Tname='張三' order by sc.score desc limit 0,1 )
select Sname,year(now())-year(Sage) as age from Student
select Sname,year(now())-year(Sage)-1 as age from Student
select Sname from Student where week(now()) = week(Sage)
select Sname from Student where (week(now())+1) = week(Sage)
select Sname from Student where month(now()) = month(Sage)
select Sname from Student where (month(now())+1) = month(Sage)
select * from Student where sid in (select sid from SC where cid in (select sc.Cid from SC where sc.Sid='01') and sid <>'01' group by sid having count(cid) >= 3 )