简体   繁体   中英

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:

  1. Violin
  2. Viola
  3. Harp
  4. Piccolo

Event_Orchestra_Musicians is a look up table to join musicians to an instrument ie:

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). 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. 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?

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. 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.

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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