[MySQL] Need Query help

Discussion in 'MySQL' started by 123GoToAndPlay, Jan 21, 2009.

  1. #1
    I am trying to display the possible answers to a poll question

    When i use the following query i get 30 records back (when i expect 6 records)

    $query_getPoll = "SELECT *, pollQuestions.content as question, pollOptions.content as option FROM pollQuestions,  pollOptions, pollVotes WHERE pollOptions.question_id = pollQuestions.question_id = pollVotes.question_id = '".$id_sent."' AND pollQuestions.displayed = '1' ";
    Code (markup):

    And when i group by question_id i get only 1 record back
    $query_getPoll = "SELECT *, pollQuestions.content as question, pollOptions.content as option FROM pollQuestions,  pollOptions, pollVotes WHERE pollOptions.question_id = pollQuestions.question_id = pollVotes.question_id = '".$id_sent."' AND pollQuestions.displayed = '1' GROUP BY  pollQuestions.question_id";
    Code (markup):
    What am i overlooking?
     
    123GoToAndPlay, Jan 21, 2009 IP
  2. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #2
    select *, fields? that incorrect

    $query_getPoll = "SELECT pollQuestions.content as question, pollOptions.content as option FROM pollQuestions, pollOptions, pollVotes WHERE pollOptions.question_id = pollQuestions.question_id = pollVotes.question_id = '".$id_sent."' AND pollQuestions.displayed = '1' ";

    I dont know whats with pollQuestions.displayed = '1' but I would use LIMIT 1 instead
     
    crivion, Jan 21, 2009 IP
  3. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #3
    SELECT * -- whatever you want to get
    FROM pollOptions
    LEFT JOIN pollQuestions ON pollOptions.question_id = pollQuestions.question_id
    WHERE
        pollQuestions.question_id = '$id_sent' AND
        pollQuestions.displayed = '1'
    Code (markup):
    pollVotes doesn't seem to be required in the query.
     
    phper, Jan 21, 2009 IP
  4. chisara

    chisara Peon

    Messages:
    141
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Can you give us your database /table design
     
    chisara, Jan 22, 2009 IP