how to retrive null data from Left Join in mysql when right table has no matching row?

40 Views Asked by At

I have two tables like these: (Docs)

Id Name
1 Doc1
2 Doc2

and

(Signs)

Id personId docId SignedAt
1 1 1 2023-01-01
2 3 2 2023-01-02

So I want to get details of docs for each user. for instance I need a list to show that user with Id = 1 like this:

(result)

DocId signedAt
1 2023-01-01
2 null

I tried Left Join like below:


SELECT * from docs
LEFT JOIN Signs on docid = docs.id 
where Signs.personId = 1 || Signs.personId is null;

but it just returns the first line of the result table. how could I generate my desired result?

1

There are 1 best solutions below

3
Akina On BEST ANSWER
SELECT t1.id, t2.signed_at
FROM docs t1
LEFT JOIN signs t2 ON t1.id = t2.doc_id AND t2.person_id = 1