开发者

SQL Query Problem With Three Tables

I've got three tables, I want to select two columns from the first table and one from the last table to be displayed in a gridview for asp.net

So I have a table Sprint, Task and Blocked.

I want to display CurrentStatus from blocked table and to do this I need to use the Task table to join them together. I've attempted the query below but it doesn't 开发者_运维技巧work.

Can anyone help me on this please, it would be appreciated.

Thanks


The way that you are doing it now, you are doing

  • first a LEFT JOIN on Task (which keeps the Sprint entries without a Task) and
  • then an INNER JOIN on Blocked (which removes the Sprint entries which don't have a Blocked entry).

Basically, you need to INNER JOIN Task and Blocked first, then RIGHT JOIN to Sprint (which gets all Sprints):

SELECT tblSprint.sprintID, tblSprint.projectID, tblBlocked.CurrentStatus 
FROM tblTask 
     INNER JOIN tblBlocked ON tblTask.taskID = tblBlocked.taskID
     RIGHT JOIN tblSprint ON tblTask.sprintID = tblSprint.sprintID

Alternatively, if that's easier to understand for you, you can write it like this: You LEFT JOIN Sprint to the already combined Task-Blocked data.

SELECT tblSprint.sprintID, tblSprint.projectID, tblBlocked.CurrentStatus 
FROM tblSprint LEFT JOIN (tblTask INNER JOIN tblBlocked 
                          ON tblTask.taskID = tblBlocked.taskID)
     ON tblSprint.sprintID = tblTask.sprintID 


Well RPM is right. I just tested it, and you need two LEFT JOINs.


I think I'm just showing what RPM1984 and Jordan are saying here and I think they're correct:

Try this:

SELECT tblSprint.sprintID, tblSprint.projectID, tblBlocked.CurrentStatus  
FROM tblSprint  
     LEFT JOIN tblTask ON tblSprint.sprintID = tblTask.sprintID  
     LEFT JOIN tblBlocked ON  tblBlocked.taskID = tblTask.taskID
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜