0

I am trying to write a query that connects 3 tables.

The first table is info about each festival. The second table is the number of votes for each festival. The third table is reviews for each festival.

I want to join all 3 tables so I get all the columns from table1, join table1 with table2 on the festivalid, but I also need to count the number of records in table 3 that apply to each festival.

The first 2 tables give me a result because they both have data in them but table 3 is empty because there are no reviews yet so adding that to my query gives me no results.

SELECT  f.*,
        v.total,
        v.votes,
        v.festivalid,
        r.reviewcount as count
    FROM festivals f
INNER
    JOIN vote v
        ON f.festivalid = v.festivalid
INNER 
    JOIN (SELECT festivalid,
                 count(*) as reviewcount
            FROM reviews)
            GROUP BY festivalid) as r

        on r.festivalid = v.festivalid
0

4 Answers 4

3
LEFT OUTER JOIN

e.g.

SELECT  f.*, 
        v.total, 
        v.votes, 
        v.festivalid, 
        r.reviewcount as count 
    FROM festivals f 
INNER 
    JOIN vote v 
        ON f.festivalid = v.festivalid 
LEFT OUTER
    JOIN (SELECT festivalid, 
                 count(*) as reviewcount 
            FROM reviews 
            GROUP BY festivalid) as r 

        on r.festivalid = v.festivalid 
2
  • Which ever one i try I get this error "Every derived table must have its own alias"
    – AdRock
    Mar 17, 2010 at 13:40
  • Sorry, that appears to be an error in your original query. the ")" after "FROM reviews". I have corrected mine.
    – Robin Day
    Mar 17, 2010 at 13:57
2
SELECT  f.*,
        v.total,
        v.votes,
        v.festivalid,
        ifnull(r.reviewcount,0) as count
    FROM festivals f
INNER
    JOIN vote v
        ON f.festivalid = v.festivalid
LEFT OUTER 
    JOIN (SELECT festivalid,
                 count(*) as reviewcount
            FROM reviews)
            GROUP BY festivalid) as r

        on r.festivalid = v.festivalid

added ifnull to show reviews as 0 in case there are none.

1
  • Many thanks. The ifnull sorted out another problem i came across
    – AdRock
    Mar 17, 2010 at 13:49
1

The second join shouldn't be an inner join.

SELECT  f.*,
            v.total,
            v.votes,
            v.festivalid,
            r.reviewcount as count
        FROM festivals f
    INNER
        JOIN vote v
            ON f.festivalid = v.festivalid
    LEFT OUTER
        JOIN (SELECT festivalid,
                     count(*) as reviewcount
                FROM reviews
                GROUP BY festivalid) as r

            on r.festivalid = v.festivalid

Article on outer joins: http://msdn.microsoft.com/en-us/library/ms187518.aspx

1
  • +1. One closing parenthesis too much (after FROM). Also if NULLs are not desired, one should do COALESCE(r.reviewcount, 0) as count in select clause
    – van
    Mar 17, 2010 at 13:32
0

Try using a outer join on your review count table (r)

SELECT  f.*,
        v.total,
        v.votes,
        v.festivalid,
        r.reviewcount as count
    FROM festivals f
INNER
    JOIN vote v
        ON f.festivalid = v.festivalid
LEFT OUTER
    JOIN (SELECT festivalid,
                 count(*) as reviewcount
            FROM reviews)
            GROUP BY festivalid) as r
        on r.festivalid = v.festivalid

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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