I have the following tables, where in the first I have procedures and in the second I register the users who read them according to the click on the procedures:
CREATE TABLE Faq ( id INT, Titulo varchar(30) );
INSERT INTO Faq VALUES
(1, 'Teste'),
(2, 'Teste1'),
(3, 'Teste2'),
(4, 'Teste3'),
(5, 'Teste4');
CREATE TABLE LeituraFaq( id INT, Id_Faq INT, leitura INT, Colaborador INT );
INSERT INTO LeituraFaq VALUES
(1, 3, 1, 19),
(2, 5, 1, 19),
(1, 3, 1, 24),
(1, 2, 1, 24);
What I intend with the two tables is to understand the procedures that each one has already read and the procedures that still have to be read.
I have the following query that returns what each contributor has already read, like this:
SELECT Faq.id, Titulo, LeituraFaq.Colaborador, leitura
FROM Faq LEFT OUTER JOIN LeituraFaq ON LeituraFaq.Id_Faq = Faq.id
WHERE LeituraFaq.Colaborador = '19'
In addition to the procedures already read, I also need the query to return the procedures that each user has not yet read.
The result I want for example for the user with id 19:
id Titulo Colaborador leitura
1 Teste
2 Teste1
3 Teste2 19 1
4 Teste3
5 Teste4 19 1
It means that the user with id 19 has already read two procedures and has 3 to read. This is the intended result. The result must be dynamic and per user.
Here is an example of what I have:
2
Answers
It seems that you need in either
or
Explanation.
Subquery
t2
gathers complete Colaborador list.Cross join with
t1
produces all possible pairs (Titulo-Colaborador).LEFT JOIN t3
with furtherWHERE .. IS NULL
selects all above pairs which have no according row int3
table.If the table which stores complete Colaborador list exists then you’d use it instead of subquery
t2
.You can do it using
LEFT JOIN
to join your tables, then useGROUP BY
andMAX CASE WHEN
to get your desired output as follows :Demo here