SQL Query to extract tasks with subtasks [duplicate]

Clash Royale CLAN TAG#URR8PPP
SQL Query to extract tasks with subtasks [duplicate]
This question already has an answer here:
Hi I'm new to MySQL and working on a small project.
I've got 3 tables:
+-------------+ +--------------+ +--------------+
| Project | | Task | | Subtask |
+-------------+ +--------------+ +--------------+
| Pname | | Tname | | Sname |
+-------------+ | pname (FK) | | Tname (FK) |
+--------------+ | pname (FK) |
+--------------+
I need an SQL query to display all the tasks along with their subtasks that belong to a particular project. In the form (task, subtask)
If a task doesn't have a subtask then it's corresponding entry should be NULL.
I know this is stupid but here's what I tried,
tasks
subtasks
task
subtask
task
SELECT Tname, Sname from Task, Subtask WHERE pname='some project'
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Try using LEFT JOIN.
– cars10m
Aug 12 at 5:56
1 Answer
1
What you need is a LEFT JOIN so that you will still get tasks which have no sub-tasks. Try this:
LEFT JOIN
SELECT T.Tname, S.Sname
FROM Task T
LEFT JOIN Subtask S ON S.TName = T.TName
WHERE T.PName = 'some project'
Welcome to Stack Overflow! Take a moment to read through the editing help in the help center. Formatting on Stack Overflow is different than other sites. The better your post looks, the easier it is for others to read and understand it.
– FrankerZ
Aug 12 at 5:51