一道值得小心的SQL題目

2021-04-17 21:57:57 字數 1853 閱讀 1196

一道值得小心的sql題目

現有三個表student(學生表)[ stuid 學生號,stuname 姓名 ],

subject(課程表)[ subid 課程號,subname 課程名 ],  

score(成績表)[ scoreid 成績記錄號,subid 課程號,stuid 學生號,score 成績 ]。

請用sql實現:

姓名   英語   數學   語文  歷史

張三   80     95     89    76

李四   90     72     84    96

create table student

(stuid int identity(1001,1) primary key,

stuname varchar(20)

)insert into student values('張三')

insert into student values('李四')

create table subject

(subid int identity(1001,1) primary key,

subname varchar(20)

)insert into subject values('英語')

insert into subject values('數學')

insert into subject values('語文')

insert into subject values('歷史')

create table score

(scoreid int identity(1001,1) primary key,

subid int,

stuid int,

score int

)insert into score values(1001,1001,80)

insert into score values(1002,1001,95)

insert into score values(1003,1001,89)

insert into score values(1004,1001,76)

insert into score values(1001,1002,90)

insert into score values(1002,1002,72)

insert into score values(1003,1002,84)

insert into score values(1004,1002,96)

select name,sum(ying)as 英語,sum(shu)as 數學,sum(yu)as 語文,sum(li) as 歷史 from

(select stuname as name ,(case when score.subid=1001 then score else 0 end)as ying ,

(case when score.subid=1002 then score else  0 end)as shu ,

(case when score.subid=1003 then score else  0 end)as yu ,

(case when score.subid=1004 then score else  0 end)as li

from score,student,subject

where score.subid=subject.subid and score.stuid = student.stuid) as tab

group by name

姓名   英語   數學   語文  歷史

張三   80     95     89    76

李四   90     72     84    96

一道SQL查詢的題目

一是查詢a id,name 表中第31至40條記錄,id作為主鍵可能是不是連續增長的列,完整的查詢語句如下 select top 10 from a where id select max id from select top 30 id from a order by a t order by a...

一道sql題目,列轉行

表num共乙個int型別欄位create table num n int 請分別寫出一下兩種情況輸出的語句。情況1c1 c2c312 3456 789情況2 c1c2c31 5926 3748 題的意思應該是表有一列num,num有1 9,9行,要按照上面格式輸出 create table num ...

一道this的題目

請問下面 中的this值指向的是全域性物件還是物件o?function f return c var o new f console.log o.constructor.name object這裡的this指向全域性物件,因為 c call without new。這裡用正常的方式呼叫的函式 c 所...