DauntlessRob
DauntlessRob

Reputation: 795

MySQL JOIN a table, and then JOIN that new table to itself

First of all, I'm not proficient at all in SQL, so this is a monumental task, but here is what I got so far:

SQL Fiddle

As you can see, I've got a project management software project and I'm trying to build a report from it. Seems like mostly I'm getting the data i need, except that I can't get the Parent Task name.

Tasks table includes ids, names, etc, as well as a column specifically for a Parent Task Id. So it needs to be joined to itself so that I can get the name of that parent task. But in the same run, I'm starting from the timelogs table, and trying to join a bunch of other tables for other information.

In my production database, for example, I also have a company table that all of the projects have an id for, so I'm going to need to join those together at some point too.

So here's the QUESTION:

Why in that sqlfiddle up there, does the parent task name field not fill out for taskID 6? How do I get that info?

Upvotes: 1

Views: 58

Answers (1)

Blue
Blue

Reputation: 22911

The left join needs to join on the pt reference you set for tasks. Your join should look like so:

LEFT JOIN tasks pt ON tasks.parentTaskId = pt.taskId

See updated fiddle here.

Upvotes: 2

Related Questions