1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Get results NOT in 2 tables - a little lost!

Discussion in 'MySQL' started by medialab, Dec 25, 2020.

  1. #1
    Hey Everyone,

    I am having an issues getting results from a JOINED table, any help would be greatly appreciated. I have 2 tables, and I need to grab results where a user has NOT answered a question:

    questions (table)

    ------------------------------
    | question_id | active |
    ------------------------------
    | 123456 | 1 |
    ------------------------------
    | 123457 | 1 |
    ------------------------------
    | 123458 | 1 |
    ------------------------------

    results (table)

    ------------------------------
    | question_id | user_id |
    ------------------------------
    | 123456 | 45 |
    ------------------------------
    | 123457 | 23 |
    ------------------------------
    | 123456 | 23 |
    ------------------------------

    I want to grab questions that a certain user has NOT answered. In the example above user_id 23 has answered question_id 123456 & 123457 - they have not answered 123458.

    Here's my current code, but it's not working and I don't know why??

    SELECT questions.question_id FROM questions LEFT JOIN results ON (questions.question_id = results.question_id) WHERE questions.active = '1' AND results.user_id != 'USERID' OR results.user_id IS NULL GROUP BY questions.question_id ORDER BY questions.question_id DESC
     
    medialab, Dec 25, 2020 IP
  2. mmerlinn

    mmerlinn Prominent Member

    Messages:
    3,197
    Likes Received:
    818
    Best Answers:
    7
    Trophy Points:
    320
    #2
    So, what results is your code producing that you do NOT want? Kind of hard to debug something if we do not have a place to start.
     
    mmerlinn, Dec 26, 2020 IP
  3. medialab

    medialab Well-Known Member

    Messages:
    366
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    138
    Digital Goods:
    1
    #3
    That's what's odd, it looks correct, but once a user answers a question it just spits out the exact same question again. They are only suppose to answer each question 1 time.
     
    medialab, Dec 26, 2020 IP
  4. medialab

    medialab Well-Known Member

    Messages:
    366
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    138
    Digital Goods:
    1
    #4
    It looks like when another user answer the same question it assumes that since it's joined and that user's ID is not the same or not NULL, it pops the question back up again - which is why the question just keeps coming back up over and over again. I need something that checks the question for a users response and then gets the next question.
     
    medialab, Dec 26, 2020 IP