如何在查询中添加另一个JOIN语句?

时间:2016-01-12 13:45:41

标签: sql asp.net gridview

我有一个.aspx页面,上面有一个gridview项目。 gridview最初正确显示以下查询:

CommandText = "SELECT AnalyticsHub_Requests.*, 
    AnalyticsHub_StatusCodes.ShortDescription AS StatusText 
    FROM AnalyticsHub_Requests 
    LEFT JOIN AnalyticsHub_StatusCodes 
    ON (AnalyticsHub_Requests.StatusId=AnalyticsHub_StatusCodes.Id) 
    WHERE AnalyticsHub_Requests.StatusId IN (4,6)";

我想显示来自另一个表的更多信息,以便列出分配给该请求的人员。我这样想:

CommandText = "SELECT AnalyticsHub_Requests.*, AnalyticsHub_StatusCodes.ShortDescription 
               AS StatusText 
               FROM AnalyticsHub_Requests 
               LEFT JOIN AnalyticsHub_StatusCodes 
               ON (AnalyticsHub_Requests.StatusId=AnalyticsHub_StatusCodes.Id) 
               WHERE AnalyticsHub_Requests.StatusId IN (4,6) , AnalyticsHub_Assignees.* 
              LEFT JOIN AnalyticsHub_Requests 
              ON (AnalyticsHub_Assignees.AssigneeId=AnalyticsHub_Requests.AssigneeId) 
              WHERE AnalyticsHub_Requests.AssigneeId IN (1,2,3,4,5,6)";

当我试图让它显示受让人信息时,根本不会显示任何内容。如何让它在表格中显示受让人信息?

1 个答案:

答案 0 :(得分:1)

您的SQL语法在几个地方都是错误的。我从您的查询中了解到,您希望AnalyticsHub_Requests LEFT JOIN AnalyticsHub_StatusCodes AnalyticsHub_Assignees LEFT JOIN AnalyticsHub_Requests SELECT AnalyticsHub_Requests.*, AnalyticsHub_StatusCodes.ShortDescription AS StatusText, AnalyticsHub_Assignees.* FROM AnalyticsHub_Requests LEFT JOIN AnalyticsHub_StatusCodes ON (AnalyticsHub_Requests.StatusId=AnalyticsHub_StatusCodes.Id) RIGHT JOIN AnalyticsHub_Assignees ON (AnalyticsHub_Assignees.AssigneeId=AnalyticsHub_Requests.AssigneeId) WHERE AnalyticsHub_Requests.StatusId IN (4,6) AND AnalyticsHub_Requests.AssigneeId IN (1,2,3,4,5,6) for (int i = 0; i < 45; i++) { if (srr[i] == 0) break; System.out.println(srr[i]); } 。你可以通过这个SQL来做到这一点。

    for (int i = 0; i < cases; i++) {
        System.out.println(srr[i]);

    }