简体   繁体   中英

Sql join query and select one more column

I have three tables : answers , questions and users and I have joined them all to get the result that I want

SELECT answers.answer_content as answer_content,
       answers.id as answer_id,
       answers.created_at as created_at,
       questions.id as question_id, 
       questions.question_title as question_title, 
       questions.question_slug as question_slug, 
       users.id as user_id, 
       users.name as user_name, 
       users.user_slug as user_slug,
                                  FROM answers
                                          JOIN questions
                                               on questions.id = answers.question_id
                                          JOIN users
                                               on users.id = answers.user_id
                                          WHERE questions.question_active = 1 and 
                                                answers.answer_active = 1

And Now I have one more table named upvote_answers . This table basically stores upvote, downvote for an answer by a user.

if a user has upvoted an answer ie Columns are user_id , answer_id , upvote (1 or 0 - upvote or downvote respectively)

So my question is : I want to get one more column with a boolean result that should show upvoted or downvoted or nothing for each answer for current logged in user

I tried writing the query like this

SELECT answers.answer_content as answer_content,
       answers.id as answer_id,
       answers.created_at as created_at,
       questions.id as question_id, 
       questions.question_title as question_title, 
       questions.question_slug as question_slug, 
       users.id as user_id, 
       users.name as user_name, 
       users.user_slug as user_slug,
       (SELECT count(upvote_answers.answer_id) from upvote_answers, answers WHERE upvote_answers.user_id = 2 and answers.id = upvote_answers.answer_id) as upvotedOrNot
                                  FROM answers
                                          JOIN questions
                                               on questions.id = answers.question_id
                                          JOIN users
                                               on users.id = answers.user_id
                                          WHERE questions.question_active = 1 and 
                                                answers.answer_active = 1

when User loggs in he should be able to see if he has upvoted or downvoted an answer or not.

Considering that a given user can only vote once on any answer, this should do the trick:

SELECT answers.answer_content as answer_content,
       answers.id as answer_id,
       answers.created_at as created_at,
       questions.id as question_id, 
       questions.question_title as question_title, 
       questions.question_slug as question_slug, 
       users.id as user_id, 
       users.name as user_name, 
       users.user_slug as user_slug,
       upvote_answers.upvote as upvote
FROM 
    answers
    JOIN questions on questions.id = answers.question_id
    JOIN users on users.id = answers.user_id
    LEFT JOIN upvote_answers ON
        upvote_answers.answer_id = answers.id AND
        upvote_answers.user_id = answers.user_id
WHERE 
    questions.question_active = 1 and 
    answers.answer_active = 1

The LEFT JOIN clause will yield results if it matches, but will bring NULL values (for its table) if it does not.

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