iT邦幫忙

0

SQL查詢結合多個資料表

sql

語法如下:

SELECT * FROM student AS a JOIN score AS b JOIN course AS c ON a.stud_id = b.stud_id AND b.c_id = c.c_id;

請問這句SQL的圖形是如下圖嗎?
http://ithelp.ithome.com.tw/upload/images/20160620/20095476fSJg19ZgfF.png
謝謝

JOIN 有分: INNER JOIN、LEFT JOIN、RIGHT JOIN、CROSS JOIN。你先搞清楚你的資料庫伺服器預設是哪種 JOIN 就能知道答案了,
peter109 iT邦新手 4 級 ‧ 2016-07-01 11:17:51 檢舉
那請問MS SQL Server是用哪一種呢?謝謝
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
4
一級屠豬士
iT邦大師 1 級 ‧ 2016-06-20 11:49:57
最佳解答

三表的交集, 當然不是你那個圖.

現在這裡都不好貼Code了.

只好放張圖, 簡單測試就能知道的東西.

http://ithelp.ithome.com.tw/upload/images/20160620/20050647lzZVfQsFab.png

看更多先前的回應...收起先前的回應...
peter109 iT邦新手 4 級 ‧ 2016-06-20 12:10:03 檢舉

不好意思,我看不太懂
能否用小畫家,畫一下圖
應該一分鐘就可以畫完。
或是告訴我,我的圖哪裡錯了,
我再畫一張上來
看是否正確
謝謝

賽門 iT邦超人 1 級 ‧ 2016-06-20 12:57:05 檢舉

http://ithelp.ithome.com.tw/upload/images/20160620/200176494AjErq5saZ.png

這樣對不對呢?你想想看吧....

peter109 iT邦新手 4 級 ‧ 2016-06-20 13:21:20 檢舉

你的意思是指下圖嗎?
可是這樣我覺得怪怪的
http://ithelp.ithome.com.tw/upload/images/20160620/20095476b03lMFWhfh.png

http://ithelp.ithome.com.tw/upload/images/20160620/20050647y4obbGoZ2C.png

左邊的

右邊的語法跟結果如下:
select id as a_join_b
     , null as a_join_c
  from ithelp160620a
  join ithelp160620b
 using (id)
 union all
select null as a_join_b
     , id as a_join_c
  from ithelp160620a
  join ithelp160620c
 using (id);

┌──────────┬──────────┐
│ a_join_b │ a_join_c │
├──────────┼──────────┤
│        2 │   [Null] │
│        3 │   [Null] │
│        4 │   [Null] │
│   [Null] │        3 │
│   [Null] │        4 │
│   [Null] │        5 │
│   [Null] │        6 │
└──────────┴──────────┘
賽門 iT邦超人 1 級 ‧ 2016-06-20 14:25:29 檢舉

你要這麼思考,A先和B進行Join,A和B Join的結果再和C進行Join,最後就是A、B和C和交集結果。

peter109 iT邦新手 4 級 ‧ 2016-06-21 11:53:58 檢舉

謝謝指導

2
尼克
iT邦大師 1 級 ‧ 2016-06-20 11:33:55

SQL JOIN

尼克 iT邦大師 1 級 ‧ 2016-06-20 11:35:38 檢舉
peter109 iT邦新手 4 級 ‧ 2016-06-20 12:05:17 檢舉

那兩張表的
我看的懂
我問的是三張表的問題

fillano iT邦超人 1 級 ‧ 2016-06-20 13:08:43 檢舉

A B C的交集啊,你畫的圖結果是空集合。如果有結果,會是A B C三個疊在一起的部份,畫成圖的話,就像一個胖胖的三角形。

0
賽門
iT邦超人 1 級 ‧ 2016-06-20 11:46:21

SQL Server是給你一個讚....呃...不....一個Error Message....

0
yuanshang
iT邦新手 4 級 ‧ 2016-06-20 17:00:01

我猜您的圖表示了:

SELECT * FROM student AS a JOIN score AS b JOIN course AS c ON (a.stud_id = b.stud_id) OR (b.c_id = c.c_id);

peter109 iT邦新手 4 級 ‧ 2016-06-21 11:52:21 檢舉

謝謝!

我要發表回答

立即登入回答