Mathieu P
Mathieu P

Reputation: 31

Mysql Get rows that are not used yet

I need some help with my query.

I need to select data that is not selected in another query.

So what is mean is:
Table 1 have 50 Questions
Table 2 have selected 32
Then there are 18 not used.
I only need to select that 18 not used questions.

Hope you can help me!

Edit:
Table with all Questions:
Id - InputType - InputName - InputLabel

Table with the selected questions:
Id - required - position

Relations: Id with Id

Upvotes: 1

Views: 65

Answers (5)

Mathieu P
Mathieu P

Reputation: 31

Final answer what i made with all your alswers.

SELECT fbb.*
FROM formbuildingblock fbb
LEFT JOIN formbbformtemplate fbbft
ON fbbft.formBuildingBlockId = fbb.formBuildingBlockId
WHERE fbbft.formBuildingBlockId IS NULL;

Upvotes: 0

Raging Bull
Raging Bull

Reputation: 18747

You can use LEFT JOIN:

SELECT T1.*
FROM Table1 T1 LEFT JOIN
     Table2 T2 ON T1.Id=T2.Id
WHERE T2.required IS NULL

Explanation:

When we join those tables with LEFT JOIN, it will select all records from Table1 and corresponding records from Table2 (if any). And we are excluding the questions which are already in Table2.

Consider the table data:

Table1                       Table2
--------------------------------------------------
id    Question               id     Question
1     Question1              1      Question1
2     Question2              3      Question3
3     Question3              5      Question5
4     Question4              
5     Question5              
6     Question6              

Then this query will result:

id    Question   
-----------------                 
2     Question2                 
4     Question4 
6     Question6                   

Upvotes: 2

Dgan
Dgan

Reputation: 10295

You can use Except

select those questions in First table but not in Second Table

select QuiestionID from Questions
except
select QuiestionID from SelectedQuestions

Upvotes: 0

Muhammad Raheel
Muhammad Raheel

Reputation: 19882

SELECT 
    aq.* 
FROM 
all_questions aq
LEFT JOIN selected_questions sq ON sq.Id = aq.Id
WHERE sq.Id IS NULL

Upvotes: 1

husnain_sys
husnain_sys

Reputation: 571

From your statement, it looks like used questions are stored in another table Table2. So it would be something like;

Select * From Table1 A
Left Join Table2 B On A.QuestionID = B.QuestionID
Where B.QuestionID is Null

Upvotes: 0

Related Questions