仅当特定列的值为 1 时才考虑内部联接,如果该特定列的值为 2 则不考虑联接
Consider a inner join only if value from a particular column is 1 and don't consider the join if the value of that particular column is 2
如果特定列的值为 1,则强制考虑内部连接子查询,否则如果列值为 2,则甚至不考虑内部连接。
如果特定列的值为 1,则强制考虑内部连接子查询,否则如果其值为 2,则不考虑内部连接。
SELECT DISTINCT Ta.aFI, Ta.cid, Ta.applicationno, dbk.bsg, dbk.cis, Ta.time
FROM ArrowAged Ta
INNER JOIN infy sc WITH (NOLOCK) ON sc.id = Ta.applicationno
INNER JOIN Intelligent ING WITH (NOLOCK) ON sc.serverid = ING.serverid AND sc.typeid = ING.typeid
LEFT JOIN IntelligentProp IP WITH (NOLOCK) ON ING.id = IP.cnid AND IP.attrName = 'sc table' AND IP.changed = 0
INNER JOIN DBKnowledge dbk WITH (NOLOCK) ON dbk.bSID = sc.bS AND ((ISNULL(IP.value, 0) = 0) OR dbk.databasename = sc.uniqueID)
INNER JOIN (SELECT DISTINCT applicationno FROM impObjects WITH (NOLOCK) WHERE objectType in (2,6) and name = 'Version1' and checksumid = CHECKSUM(N'Version1')) AF ON AF.applicationno = Ta.applicationno
现在在查询中看到有这个语句
INNER JOIN (SELECT DISTINCT applicationno FROM impObjects WITH (NOLOCK) WHERE objectType in (2,6) and name = 'Version1' and checksumid = CHECKSUM(N'Version1')) AF ON AF.applicationno = Ta.applicationno
where 如果etype 为1 则强制考虑inner join 和里面的子查询条件,否则不考虑inner join 语句。 etype 1 必须跟在子查询、内连接和条件之后。
例如:
case dbk.etype = 1
INNER JOIN (SELECT DISTINCT applicationno FROM impObjects WITH (NOLOCK) WHERE objectType in (2,6) and name = 'Version1' and checksumid = CHECKSUM(N'Version1')) AF ON AF.applicationno = Ta.applicationno
case dbk.etype = 2
DON'T CONSIDER THE INNER JOIN STATEMENT
注意:我不想使用 case 语句
使用 LEFT
连接代替 INNER
连接并在 ON
子句中设置条件:
LEFT JOIN (
SELECT DISTINCT applicationno
FROM impObjects WITH (NOLOCK)
WHERE objectType in (2,6) AND name = 'Version1' AND checksumid = CHECKSUM(N'Version1')
) AF ON AF.applicationno = Ta.applicationno AND dbk.etype = 1
并添加一个 WHERE
子句:
WHERE dbk.etype IN (1, 2)
因为你实际上并没有从那个子查询中选择任何东西,所以当指定为半连接时会好得多,例如 EXISTS
. 然后你可以简单地使用 OR
:
WHERE dbk.etype = 2
OR EXISTS (SELECT 1
FROM impObjects AF
WHERE AF.objectType in (2, 6)
AND AF.name = 'Version1'
AND AF.checksumid = CHECKSUM(N'Version1')
AND AF.applicationno = Ta.applicationno -- this line is the join condition
)
或者,如果 etype
有更多可能的值,则:
WHERE dbk.etype = 2
OR (dbk.etype = 1 AND EXISTS (SELECT 1
FROM impObjects AF
WHERE AF.objectType in (2, 6)
AND AF.name = 'Version1'
AND AF.checksumid = CHECKSUM(N'Version1')
AND AF.applicationno = Ta.applicationno -- this line is the join condition
))
如果特定列的值为 1,则强制考虑内部连接子查询,否则如果列值为 2,则甚至不考虑内部连接。 如果特定列的值为 1,则强制考虑内部连接子查询,否则如果其值为 2,则不考虑内部连接。
SELECT DISTINCT Ta.aFI, Ta.cid, Ta.applicationno, dbk.bsg, dbk.cis, Ta.time
FROM ArrowAged Ta
INNER JOIN infy sc WITH (NOLOCK) ON sc.id = Ta.applicationno
INNER JOIN Intelligent ING WITH (NOLOCK) ON sc.serverid = ING.serverid AND sc.typeid = ING.typeid
LEFT JOIN IntelligentProp IP WITH (NOLOCK) ON ING.id = IP.cnid AND IP.attrName = 'sc table' AND IP.changed = 0
INNER JOIN DBKnowledge dbk WITH (NOLOCK) ON dbk.bSID = sc.bS AND ((ISNULL(IP.value, 0) = 0) OR dbk.databasename = sc.uniqueID)
INNER JOIN (SELECT DISTINCT applicationno FROM impObjects WITH (NOLOCK) WHERE objectType in (2,6) and name = 'Version1' and checksumid = CHECKSUM(N'Version1')) AF ON AF.applicationno = Ta.applicationno
现在在查询中看到有这个语句
INNER JOIN (SELECT DISTINCT applicationno FROM impObjects WITH (NOLOCK) WHERE objectType in (2,6) and name = 'Version1' and checksumid = CHECKSUM(N'Version1')) AF ON AF.applicationno = Ta.applicationno
where 如果etype 为1 则强制考虑inner join 和里面的子查询条件,否则不考虑inner join 语句。 etype 1 必须跟在子查询、内连接和条件之后。
例如:
case dbk.etype = 1
INNER JOIN (SELECT DISTINCT applicationno FROM impObjects WITH (NOLOCK) WHERE objectType in (2,6) and name = 'Version1' and checksumid = CHECKSUM(N'Version1')) AF ON AF.applicationno = Ta.applicationno
case dbk.etype = 2
DON'T CONSIDER THE INNER JOIN STATEMENT
注意:我不想使用 case 语句
使用 LEFT
连接代替 INNER
连接并在 ON
子句中设置条件:
LEFT JOIN (
SELECT DISTINCT applicationno
FROM impObjects WITH (NOLOCK)
WHERE objectType in (2,6) AND name = 'Version1' AND checksumid = CHECKSUM(N'Version1')
) AF ON AF.applicationno = Ta.applicationno AND dbk.etype = 1
并添加一个 WHERE
子句:
WHERE dbk.etype IN (1, 2)
因为你实际上并没有从那个子查询中选择任何东西,所以当指定为半连接时会好得多,例如 EXISTS
. 然后你可以简单地使用 OR
:
WHERE dbk.etype = 2
OR EXISTS (SELECT 1
FROM impObjects AF
WHERE AF.objectType in (2, 6)
AND AF.name = 'Version1'
AND AF.checksumid = CHECKSUM(N'Version1')
AND AF.applicationno = Ta.applicationno -- this line is the join condition
)
或者,如果 etype
有更多可能的值,则:
WHERE dbk.etype = 2
OR (dbk.etype = 1 AND EXISTS (SELECT 1
FROM impObjects AF
WHERE AF.objectType in (2, 6)
AND AF.name = 'Version1'
AND AF.checksumid = CHECKSUM(N'Version1')
AND AF.applicationno = Ta.applicationno -- this line is the join condition
))