0

I have two tables, answerstbl and questionstbl. All questions are inserted in questionstbl and so as answeres in the answerstbl. Now, I want to get questions that are NOT YET answered by anybody by referring to the answerstbl. This is my query:

 "SELECT * FROM questionstbl LEFT JOIN answerstbl ON answerstbl.qu_id=questionstbl.qu_id 
  GROUP BY ans_id"

However, the result is the question with answers.

Does anybody know?

smz
  • 173
  • 1
  • 14
  • Take a look at https://stackoverflow.com/questions/5706437/whats-the-difference-between-inner-join-left-join-right-join-and-full-join – PerroVerd Feb 11 '16 at 09:45

5 Answers5

1

You can use NOT IN comparison operator.

Query

Select * from questionstbl  
where qu_id not in(
    select distinct qu_id from answerstbl
);
Ullas
  • 11,450
  • 4
  • 33
  • 50
1

So you want all the questions where that question id does not exist in the answers table......

SELECT Q.*
FROM questionstbl Q
WHERE NOT EXISTS(SELECT 1 FROM answerstbl A WHERE A.qu_id = Q.qu_id )

or like this....

SELECT Q.*
FROM questionstbl Q
WHERE 
  Q.qu_id NOT IN (SELECT qu_id FROM answertbl)
AntDC
  • 1,807
  • 14
  • 23
1

the condition to be check should be entered based on the fields in your table, if you could specify your table fields the condition could be filled in.

Query

select
questionstbl.qu_id as questionId,
answerstbl.ans_id as answersId,
from questionstbl, answerstbl
LEFT JOIN answerstbl ON answerstbl.qu_id=questionstbl.qu_id 
where (condition to check questions that are NOT YET answered)
GROUP BY ans_id;
Marilynn
  • 71
  • 5
0

Something like this:

SELECT * FROM questionstbl WHERE qu_id NOT IN (SELECT qu_id FROM answerstbl)
0

Another option can be:

Select questions.qu_id, questions.question from questions INNER JOIN answers where questions.qu_id <> answers.qu_id

Above query result all questions fom questions table that are not yet answered. However, there is one mistake in your query, you want to get questions with no answers and you are grouping them with respect to answer id (where answer id does not exist for those questions).

waheed
  • 134
  • 1
  • 8