作者cutecpu (可愛中央處理器)
看板Database
標題Re: [SQL ] 詢問SQL語法
時間Thu Apr 2 15:31:47 2009
可以這樣下:
SELECT a.id , a.Student , b.Score
FROM
(
SELECT a.id , b.Student
FROM TableA a , TableB b
GROUP BY a.id , b.Student
) a
LEFT OUTER JOIN TableB b
ON a.id = b.id and a.Student = b.Student
ORDER BY a.Student ASC , a.id ASC
但如果沒有考該科目的話,Score的值是空的,我也不知道怎樣才能顯示 0
※ 引述《hammert (^^)》之銘言:
: 若我有下列二個Table
: TableA
: --------------------
: id | Name
: --------------------
: 001 | SQL
: 002 | Enligh
: 003 | Chinese
: TableB
: ------------------------
: Student | id | Score
: ------------------------
: A | 001 | 80
: A | 003 | 90
: B | 001 | 100
: B | 002 | 60
: 而我要找出的資料如下:
: 001 A 80
: 002 A 0
: 003 A 90
: 001 B 100
: 002 B 60
: 003 B 0
: 那我要怎麼寫?
: 剛剛試著把二個Table join起來,但好像永遠只有四筆資料
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.220.169.2
推 grence:group by這樣用結果等於distinct,distinct通常會比較快 04/02 21:35
→ cutecpu:嗯,原來是這樣,又學到一招 ^_^ 04/03 00:34