简体   繁体   English

我在MYSQL中的右外部联接出现问题

[英]Issue with my right outer join in MYSQL

Outer joins don't seem to work whether I use a left outer join or right outer join: 无论我使用左外连接还是右外连接,外连接似乎都无法工作:

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 contains different instruments with a unique ID ie: Orchestra_instruments包含具有唯一ID的不同乐器,即:

  1. Violin 小提琴
  2. Viola 中提琴
  3. Harp 竖琴
  4. Piccolo 短笛

Event_Orchestra_Musicians is a look up table to join musicians to an instrument ie: Event_Orchestra_Musicians是一个查找表,用于将音乐家加入乐器,即:

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

When I do any outer join, using the data in those tables, I would get the results of an inner join (Piccolo wouldn't show up with a null musician_id, it wont show up at all). 当我使用这些表中的数据进行任何外部联接时,我将获得内部联接的结果(Piccolo不会显示为null的musicer_id,也不会显示出来)。 Is there something I'm doing wrong? 我做错了什么吗?

EDIT: 编辑:

So I did some monkeying around. 所以我四处游荡。 The issue seems to be because there is a record in the events_orchestra_musicians table with an event_id of 5 and an instrument_id of 7. If I remove that record, then the outer join works. 问题似乎是因为events_orchestra_musicians表中有一条记录,其中event_id为5,instrument_id为7。如果删除该记录,则外部联接起作用。 What I don't get is if that record is there and I use the where clause to look for event_id = 2, why does it matter if there's a record in there with an instrument_id of 7 if its event_id is 5? 我没有得到的是该记录是否存在,而我使用where子句查找event_id = 2,为什么如果event_id为5,那么其中是否有一条Instrument_id为7的记录呢?

Try this: 尝试这个:

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

You have to use orchestra_instruments as the base table, since that is the one you want all of the records for, even if no musician exists. 您必须将orchestra_instruments用作基表,因为即使没有音乐家存在,它也是您想要所有记录的基表。 I can't imagine any reasons for using a Right join over a Left join, and Outer is implied. 我无法想象使用任何方法在右连接上使用右连接,并且暗示有外层。 Also, you have to allow event_id to either be 2 or null, because it cannot be 2 if there is no matching record to join. 另外,您必须允许event_id为2或为null,因为如果没有匹配的记录要加入,则不能为2。

You don't have any records in Event_Orchestra_Musicians where instrument_id=4 so you'll need to check for a null value from eom when doing a right outer join. 您在Event_Orchestra_Musicians中没有任何记录,其中instrument_id=4因此在进行正确的外部eom时需要从eom检查null值。

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

Here is my complete setup. 这是我的完整设置。 Maybe there something with your structure? 也许您的结构有些问题?

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM