Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check what values doesn't exist in SQL database

Tags:

sql

php

mysql

I have a question about MySQL table.
I have 2 tables (users (user_id and other rows) and answers (id, answer_id and user_id))

I would like to check, which questions the user hasn't answered (for example, in answers table exists 5 rows - 4,6,8,1,3 (but questions are 10), I would like to get out from database values 2,5,7,9,10).

How to write a query like this? I've tried with JOIN, but nothing was successful at all!

like image 437
Tediee Avatar asked Feb 28 '14 13:02

Tediee


1 Answers

Assuming that you have a questions and an answers table, this is the standard TSQL solution:

SELECT Q.QUESTION_ID 
FROM   QUESTIONS Q LEFT JOIN ANSWERS A ON Q.QUESTION_ID = A.QUESTION_ID 
WHERE  A.QUESTION_ID IS NULL 
like image 77
kiks73 Avatar answered Oct 03 '22 17:10

kiks73