69

I have 3 tables:

Persons (PersonID, Name, SS)
Fears (FearID, Fear)
Person_Fear (ID, PersonID, FearID)

Now I'd like to list every person with whatever fear is linked to them (can be multiple fears but can also be none). The persons table has to be shown even if a person doesn't have a fear linked to them.

I think I need to do a LEFT JOIN, but my code doesn't seem to work:

SELECT persons.name, 
       persons.ss, 
       fears.fear 
FROM   persons 
       LEFT JOIN fears 
              ON person_fear.personid = person_fear.fearid 

What am I doing wrong here?

0

6 Answers 6

92

You are trying to join Person_Fear.PersonID onto Person_Fear.FearID - This doesn't really make sense. You probably want something like:

SELECT Persons.Name, Persons.SS, Fears.Fear FROM Persons
LEFT JOIN Person_Fear
    INNER JOIN Fears
    ON Person_Fear.FearID = Fears.FearID
ON Person_Fear.PersonID = Persons.PersonID

This joins Persons onto Fears via the intermediate table Person_Fear. Because the join between Persons and Person_Fear is a LEFT JOIN, you will get all Persons records.

Alternatively:

SELECT Persons.Name, Persons.SS, Fears.Fear FROM Persons
LEFT JOIN Person_Fear ON Person_Fear.PersonID = Persons.PersonID
LEFT JOIN Fears ON Person_Fear.FearID = Fears.FearID
8
  • 1
    I downvoted because you're original post didn't reference all 3 tables. You have corrected that, but you have the nesting of the On clauses wrong.
    – Aheho
    Apr 25, 2013 at 18:40
  • +1 - I know, this looks like the only correct answer so far (after any edits at least).
    – Tom
    Apr 25, 2013 at 18:41
  • 1
    Incidentally, I'm not sure which of these would be more efficient. I would wager the latter as it eliminates the need to join rows of Person_Fear onto Fears if there's no Person to Person_Fear relationship. Can anyone provide insight?
    – Ant P
    Apr 25, 2013 at 18:49
  • 1
    @AntP In my opinion, you're delving into the category of premature optimization when you start asking questions like that. Get your indices in order, and don't sweat detail like that. A decent query optimizer will probably generate a very similar query plan for each.
    – Aheho
    Apr 25, 2013 at 18:54
  • 1
    @Aheho: Actually, no. @AntP is correct, it's a pretty good question. And many optimizers would produce different query plans. One reason being that the queries are not logically identical. (OK, they are but only if there is an FK from Person_Fear towards Fear). And MYSQL's primitive optimizer may not be aware and use that info. Apr 25, 2013 at 19:02
30

try this

    SELECT p.Name, p.SS, f.Fear 
    FROM Persons p 
    LEFT JOIN Person_Fear fp 
    ON p.PersonID = fp.PersonID
    LEFT JOIN Fear f
    ON f.FearID = fp.FearID
0
20

Try this definitely work.

SELECT p.PersonID AS person_id,
   p.Name, p.SS, 
   f.FearID AS fear_id,
   f.Fear 
   FROM person_fear AS pf 
      LEFT JOIN persons AS p ON pf.PersonID = p.PersonID 
      LEFT JOIN fears AS f ON pf.PersonID = f.FearID 
   WHERE f.FearID = pf.FearID AND p.PersonID = pf.PersonID
3
  • I couldn't get this to work, but i solved it by removing the AS variables and just using full table names
    – RozzA
    Nov 23, 2014 at 4:08
  • Can anyone comment on the performance difference between the Left, Left joins vs Left,Inner joins Sep 15, 2017 at 14:36
  • @anveshtummala -- Performance: No diff. "INNER" is ignored. But, the table is probably poorly indexed; see Many-to-many
    – Rick James
    Sep 9, 2022 at 23:01
13
Select Persons.Name, Persons.SS, Fears.Fear
From Persons
LEFT JOIN Persons_Fear
ON Persons.PersonID = Person_Fear.PersonID
LEFT JOIN Fears
ON Person_Fear.FearID = Fears.FearID;
2
  • 1
    Code-only answers are discouraged. Please describe what this code does that the OP was not achieving.
    – lit
    Jun 6, 2018 at 21:54
  • 9
    this is quite practical answer. you guys should stop discouraging newbies. add some comments yourself if you find it too code-y Jul 12, 2018 at 16:32
11
Select 
    p.Name,
    p.SS,
    f.fear
From
    Persons p
left join
        Person_Fear pf
    inner join
        Fears f
    on
        pf.fearID = f.fearID
 on
    p.personID = pf.PersonID
0
1
SELECT p.*, f.Fear
FROM Persons p
LEFT JOIN Person_Fear pf ON pf.PersonID = p.PersonID
LEFT JOIN Fears f ON f.FearID = pf.FearID
ORDER BY p.PersonID

  1. You need to select from the Persons table to ensure you generate a row for every person, whether they have fears or not.
  2. Then you can left join Person_Fear to every person, which will just be NULL if they don't have any entries (as you want).
  3. Finally, you left join Fears on Person_Fear so that you can select the name of the fear.
  4. Optionally, add an order so that each person has all their fears listed together, even if they were added to the Person_Fear table at different times.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.