2012-02-03 21 views
0

外似乎沒有聯接工作,我是否使用左外連接或右外連接:問題,我的右外MYSQL中加入

SELECT * FROM `event_orchestra_musicians` eom 
right outer join `orchestra_instruments` oi on eom.instrument_id = oi.oi_id 
where event_id = 2 
order by instrument_id 

Orchestra_instruments包含一個唯一的ID,即不同的樂器:

  1. 小提琴
  2. 中提琴
  3. 豎琴
  4. 短笛

Event_Orchestra_Musicians是一個查找表加入音樂家的儀器,即:

musician_id, instrument_id, event_id, 
1 1 2 
2 1 2 
3 3 2 
4 2 2 

當我做任何外連接,使用這些表中的數據,我會得到一個內部的結果加入(短笛不會出現一個空的musician_id,它根本不會出現)。有什麼我做錯了嗎?

編輯:

所以我做了一些胡鬧左右。這個問題似乎是因爲在events_orchestra_musicians表中有一個event_id爲5且instrument_id爲7的記錄。如果我刪除該記錄,則外連接將起作用。我沒有得到的是,如果該記錄存​​在,我使用where子句查找event_id = 2,如果其中的event_id爲5的instrument_id爲7,那麼爲什麼它存在記錄?

回答

2

試試這個:

select oi.oi_id, oi.instrument_name, eom.musician_id 
from orchestra_instruments oi 
left join event_orchestra_musicians eom on oi.oi_id = eom.instrument_id 
where (eom.event_id = 2 or eom.event_id is null) 
order by oi.oi_id 

你必須使用orchestra_instruments基表,因爲這是你想要的所有記錄的一個,即使不存在的音樂家。我無法想象任何使用Left連接的Right連接的理由,而Outer是隱含的。此外,您必須允許event_id爲2或null,因爲如果沒有匹配的記錄加入,它不能爲2。

+0

唯一必要的改變是'eom.event_id是null'部分。在他的發言中,他只要求'event_id = 2',但如果沒有匹配的音樂家,event_id顯然不是2,而是無效,因此該樂器不會返回。 – 2012-02-03 16:35:14

+0

感謝您的快速回復,我已經嘗試切換基表並且它沒有任何作用。我已經在where子句中複製了帶有和沒有null event_id的代碼,它仍然充當內部聯接,不顯示任何空值或帶過短笛。 – user1143767 2012-02-03 16:39:18

+1

@ user1143767:請顯示您現在實際使用的聲明。 Fosco提供的聲明應該有效。 – 2012-02-03 16:41:31

1

您在Event_Orchestra_Musicians其中instrument_id=4沒有任何記錄,因此您需要在執行右外連接時檢查eomnull值。

SELECT * FROM `event_orchestra_musicians` eom 
right outer join `orchestra_instruments` oi on eom.instrument_id = oi.oi_id 
where event_id = 2 or eom.instrument_id is null 
order by instrument_id 
0

這是我的完整設置。也許有你的結構的東西?

mysql> show columns from orchestra_instruments; 
+---------+-------------+------+-----+---------+-------+ 
| Field | Type  | Null | Key | Default | Extra | 
+---------+-------------+------+-----+---------+-------+ 
| oi_id | int(11)  | YES |  | NULL |  | 
| oi_name | varchar(10) | YES |  | NULL |  | 
+---------+-------------+------+-----+---------+-------+ 
2 rows in set (0.01 sec) 

mysql> show columns from event_orchestra_musicians; 
+---------------+---------+------+-----+---------+-------+ 
| Field   | Type | Null | Key | Default | Extra | 
+---------------+---------+------+-----+---------+-------+ 
| musician_id | int(11) | YES |  | NULL |  | 
| instrument_id | int(11) | YES |  | NULL |  | 
| event_id  | int(11) | YES |  | NULL |  | 
+---------------+---------+------+-----+---------+-------+ 
3 rows in set (0.01 sec) 

mysql> select * from orchestra_instruments; 
+-------+---------+ 
| oi_id | oi_name | 
+-------+---------+ 
|  1 | Violin | 
|  2 | Viola | 
|  3 | Harp | 
|  4 | Piccolo | 
+-------+---------+ 
4 rows in set (0.00 sec) 

mysql> select * from event_orchestra_musicians; 
+-------------+---------------+----------+ 
| musician_id | instrument_id | event_id | 
+-------------+---------------+----------+ 
|   1 |    1 |  2 | 
|   2 |    1 |  2 | 
|   3 |    3 |  2 | 
|   4 |    2 |  2 | 
+-------------+---------------+----------+ 
4 rows in set (0.00 sec) 

select oi.oi_id, oi.oi_name, eom.musician_id 
from orchestra_instruments oi 
left join event_orchestra_musicians eom on oi.oi_id = eom.instrument_id 
where (eom.event_id = 2 or eom.event_id is null) 
order by oi.oi_id; 

+-------+---------+-------------+ 
| oi_id | oi_name | musician_id | 
+-------+---------+-------------+ 
|  1 | Violin |   1 | 
|  1 | Violin |   2 | 
|  2 | Viola |   4 | 
|  3 | Harp |   3 | 
|  4 | Piccolo |  NULL | 
+-------+---------+-------------+ 
5 rows in set (0.00 sec) 
+0

我更新了一些發現後的原創帖子。 – user1143767 2012-02-03 17:49:03