Jump to content

Help With Join


doublet216
Go to solution Solved by doublet216,

Recommended Posts

I am trying to join 2 tables, retrieve all the data from one table as well as a column from another table on 2 where conditions. 

 

Here are my tables.

 

Table 1

 

art_id | artist_id |         file            |  competition_id  |

----------------------------------------------------------------

   1     |      2       | images/aaa.jpg |    1

   2     |      2       | images/bbb.jpg |    1

   3     |      5       | images/ccc.jpg  |    1

   4   |        5       | images/fun.jpg  |     1

 

Table 2

 

art_id | artist_id |   juror_id  |  score  |  competiton_id

----------------------------------------------------------------_

  1      |       2     |        5      |       10   |        1

 

As you can see, Table 2 has 1 entry, I need to join table 2 with table 1 to show the score.   So my end results should be.

 

art_id | artist_id |         file            |  competition_id  |   Score   |

-----------------------------------------------------------------------------

   1     |      2       | images/aaa.jpg |    1                    |      5       |

   2     |      2       | images/bbb.jpg |    1                    |      null    |

   3     |      5       | images/ccc.jpg  |    1                    |      null    |

   4   |        5       | images/fun.jpg  |    1                    |      null    |

 

 

Here is my query, but it just returns the 1 result.

 

SELECT t1.art_id, t1.artist_id, t1.file, t2.score, t2.juror_id
FROM table_one t1
RIGHT JOIN  table_two t2 ON t1.art_id = t2.art_id AND t1.artist_id = t2.artist_id
WHERE t2.juror_id = 5 AND t1.competition_id = 1

 

 

Link to comment
Share on other sites

Don't think that fixes it,  If I just do

 

SELECT t1.art_id, t1.artist_id, t1.file, t2.score, t2.juror_id
FROM table_one t1
RIGHT JOIN  table_two t2 ON t1.art_id = t2.art_id
WHERE t2.juror_id = 5 AND t1.competition_id = 1
 

I still just get the 1 result, I need to get

 

art_id | artist_id |         file            |  competition_id  |   Score   |

-----------------------------------------------------------------------------

   1     |      2       | images/aaa.jpg |    1                    |      5       |

   2     |      2       | images/bbb.jpg |    1                    |      null    |

   3     |      5       | images/ccc.jpg  |    1                    |      null    |

   4   |        5       | images/fun.jpg  |    1                    |      null    |

Link to comment
Share on other sites

Original Query

 

SELECT t1.art_id, t1.artist_id, t1.file, t2.score, t2.juror_id
FROM competition_artist_artwork t1
LEFT JOIN  competition_artwork_review t2 ON t1.art_id = t2.art_id
WHERE t2.juror_id = 5 AND t1.competition_id = 1

 

Competition Arist Artwork

 

art_id

artist_id

file

competition_id

 

art_id | artist_id |         file            |  competition_id  |

----------------------------------------------------------------

   1     |      2       | images/aaa.jpg |    1

   2     |      2       | images/bbb.jpg |    1

   3     |      5       | images/ccc.jpg  |    1

   4   |        5       | images/fun.jpg  |     1

 

 

 

Competition Artwork Review

 

art_id

artist_id

juror_id

score

competition_id

 

Table 2

 

art_id | artist_id |   juror_id  |  score  |  competiton_id

-----------------------------------------------------------------

  1      |       2     |        5      |       5   |        1

 

The oringal query should result in

 

art_id | artist_id |         file            |  competition_id  |   Score   |

-----------------------------------------------------------------------------

   1     |      2       | images/aaa.jpg |    1                    |      5       |

   2     |      2       | images/bbb.jpg |    1                    |      null    |

   3     |      5       | images/ccc.jpg  |    1                    |      null    |

   4   |        5       | images/fun.jpg  |    1                    |      null    |

Edited by doublet216
Link to comment
Share on other sites

SELECT t1.art_id, t1.artist_id, t1.file, t1.competition_id, t2.score
FROM table1 AS t1
LEFT JOIN table2 AS t2
  ON t1.art_id    = t2.art_id
 AND t1.artist_id = t2.artist_id

 

You want ALL the records from table 1, so you should not use a WHERE clause to filter out any records.

 

WHERE t2.juror_id = 5 AND t1.competition_id = 1

 

However, if you want to restrict which records from table 2 are joined on table 1 on those fields you can do that in the JOIN condition. Since some records from t1 do not have corresponding records from t2, the first condition in the where clause will exclude all of those records.

SELECT t1.art_id, t1.artist_id, t1.file, t1.competition_id, t2.score
FROM table1 AS t1
LEFT JOIN table2 AS t2
  ON t1.art_id    = t2.art_id
 AND t1.artist_id = t2.artist_id
 AND t2.juror_id = 5
 AND t1.competition_id = 1
Edited by Psycho
Link to comment
Share on other sites

It's only the t2 condition that needs to be in the JOIN clause. Conditions on t1 can stay in the WHERE

 

SELECT t1.art_id, t1.artist_id, t1.file, t1.competition_id, t2.Score
FROM table1 t1
LEFT JOIN table2 t2
    ON t1.art_id = t2.art_id
    AND t1.artist_id = t2.artist_id
    AND t2.juror_id = 5
WHERE t1.competition_id = 1
Link to comment
Share on other sites

  • Solution

 

It's only the t2 condition that needs to be in the JOIN clause. Conditions on t1 can stay in the WHERE

 

SELECT t1.art_id, t1.artist_id, t1.file, t1.competition_id, t2.Score
FROM table1 t1
LEFT JOIN table2 t2
    ON t1.art_id = t2.art_id
    AND t1.artist_id = t2.artist_id
    AND t2.juror_id = 5
WHERE t1.competition_id = 1

 

Thanks!   It's been sometime since I have done actual queries.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.