使用联接优化SQL查询

时间:2018-10-26 01:33:02

标签: sql sql-server join left-join inner-join

我使用了两个变量,每个变量带有一个exit status 1 Main.java:12: error: bad operand types for binary operator '&&' if (in.charAt(i) == ' ' && in.charAt(i + 1)); ^ first type: boolean second type: char 1 error 语句。

有人告诉我可以使用SELECT进行一次查询来优化查询。

我设法将陈述合并为一个。但是它没有使用JOIN,在我当前的查询中仍然有2条JOIN语句。

当前查询:

SELECT

我当前的查询是否行得通,还是可以进一步改善?谢谢。


上一个查询:

BEGIN

DECLARE @EntityId int, @Country nvarchar(10), @OrganizationId int, 
@Username nvarchar(100) = 'user1'

Set @OrganizationId = (SELECT BU.OrganizationId 
FROM [Company].[Config].[BusinessUnit] BU 
WHERE BU.EntityId = (SELECT BU.EntityId
FROM [Company].[PES].[EmployeeProfile] EP
LEFT JOIN   Config.BusinessUnit bu on EP.EntityCode = BU.EntityCode
WHERE EP.Username = @Username))

IF(@OrganizationId = 3)
    SET @Country = 'MS'
ELSE
    SET @Country = 'SG'
SELECT @Country

END

1 个答案:

答案 0 :(得分:2)

为什么不将其合并为一个查询?

SELECT @Country = (CASE WHEN @OrganizationId = 3 THEN 'MS'
                        ELSE 'SG'
                   END)
FROM [Company].[PES].[EmployeeProfile] EP JOIN
     Config.BusinessUnit cbu 
     ON EP.EntityCode = cbu.EntityCode JOIN
     [Company].[PES].[BusinessUnit] BU 
     ON cbu.EntityId = bu.EntityId
WHERE EP.Username = @Username)