select * from 後有多個表的使用方法

來源:互聯網
上載者:User

 

 第一題:(1) 已知一個表的結構為: ------------------- 姓名 科目 成績 張三 語文 20 張三 數學 30 張三 英語 50 李四 語文 70 李四 數學 60 李四 英語 90   怎樣通過 select 語句把他變成以下結構: ------------------------------------ 姓名 語文成績 數學成績 英語成績 張三       20          30          50 李四       70          60          90   insert into student values(' 李四 ' , ' 英語 ' , '90' ) select * from student -- --法一: 正 解如下: select A. 姓名 , A. 成績 as 語文成績 , B. 成績 as 數學成績 , C. 成績 as 英語成績 from student A,student B,student C where A. 姓名 = B. 姓名 and B. 姓名 = C. 姓名 and A. 科目 = ' 語文 ' and B. 科目 = ' 數學 ' and C. 科目 = ' 英語 '   -------- 理解如下: select * from student A,student B,student C -- 將三個相同的 student 表相互串連,串連產生6 *6*6=216 條記錄 , 因為每個表中有6條記錄。 where A. 姓名 = B. 姓名 and B. 姓名 = C. 姓名 -- 對串連表記錄進行篩選;得到 (3*3*3)+(3*3*3)=27+27=54 條記錄。 and A. 科目 = ' 語文 ' and B. 科目 = ' 數學 ' and C. 科目 = ' 英語 ' -- 同時篩選三個子表中的科目內容 , 執行可得如下。 姓名    科目  成績    姓名    科目   成績    姓名    科目   成績 張三    語文    20      張三    數學       30     張三    英語     50        李四    語文    70      李四    數學       60     李四   英語     90        再在select中定義一下輸出即可。 ---- 法二:正解如下 select姓名, sum(case科目when '語文' then 成績else 0 end) as 語文成績, sum(case科目when '數學' then 成績else 0 end) as 數學成績, sum(case科目when '英語' then 成績else 0 end) as 英語成績, avg(成績) as 平均成績,sum(成績) as 總成績 from student group by 姓名 order by 姓名desc 結果如下: 姓名 語文成績 數學成績 英語成績 平均成績 總成績 張三      20          30          50          33         100 李四      70          60          90          73          220

(2)  create table A ( year int, Quarter varchar(30), amount float ) insert A select 2000,'1',1.1 insert A select 2000,'2',1.2 insert A select 2000,'3',1.3 insert A select 2000,'4',1.4 insert A select 2001,'1',2.1 insert A select 2001,'2',2.2 insert A select 2001,'3',2.3 insert A select 2001,'4',2.4 表 A 定義如下:
屬性 類型
Year Integer
Quarter Varchar ( 30 )
Amount float

Year Quarter Amount
2000 1 1.1
2000 2 1.2
2000 3 1.3
2000 4 1.4
2001 1 2.1
2001 2 2.2
2001 3 2.3
2001 4 2.4
其中每行表表示一個季度的資料。

如果處理表 A 中的資料,得到如下的結果。
Year Quarter1 Quarter2 Quarter3 Quarter4
2000   1.1      1.2     1.3     1.4
2001   2.1      2.2     2.3     2.4
請用 SQL 寫一段代碼實現。   ---法一: 正解如下: select T1.YEAR,T1.amount as Quarter1,T2.amount as Quarter2,T3.amount as Quarter3,T4.amount as Quarter4 from A T1,A T2,A T3,A T4 WHERE T1.YEAR=T2.YEAR AND T2.YEAR=T3.YEAR AND T3.YEAR=T4.YEAR AND T1.Quarter='1' and T2.Quarter='2' and T3.Quarter='3' and T4.Quarter='4' ---法二:正解如下: select year, sum(case Quarter when '1' then Amount else 0 end) as Quarter1, sum(case Quarter when '2' then Amount else 0 end) as Quarter2, sum(case Quarter when '3' then Amount else 0 end) as Quarter3, sum(case Quarter when '4' then Amount else 0 end) as Quarter4, sum(Amount) as ALLAmount from A group by year order by year

 第二題: 有一張老師表 T(T_ID,T_NAME) ; 有一張學生表 S(S_ID,S_NAME) ; 有一張班級表 C(T_ID,S_ID,C_NAME) , 其中 C_NAME 的取值只有 ‘ 大班 ’ 和 ‘ 小班 ’ , 請查詢出合格老師的名字,條件是老師在大班中帶的學生數大於此老師在小班中帶的學生數。 (最好用子查詢吧,題目是這麼要求的,另資料庫用的是 SQL Server )   select * from T, ( select count(*) as x,T_ID from C where c_name=' 小班 ' group <

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.