在下面的现有语句中,我将host_name和ip_adress与从其他表中检索到的记录进行比较,并在找到该列时将其标记为true,但是当host_name也是其中一部分时,我想对其进行调整以提供真实结果检索记录中的identifer 即
host_name=abc
identifier=abc.google.com should also be true
CASE
WHEN
(
(`inv_y`.`host_name` <> '')
AND
`inv_y`.`host_name` IN (SELECT `inv_x`.`identifier`
FROM `inv_x`
ORDER BY `inv_x`.`creation_date` DESC
)
)
OR
(
(`inv_y`.`ip_address` <> '')
AND
inv_y`.`ip_address` IN (SELECT `inv_x`.`identifier`
FROM `inv_x`
ORDER BY `inv_x`.`creation_date` DESC
)
)
THEN
'True'
ELSE
'False'
END
答案 0 :(得分:0)
您似乎可以从EXISTS
函数中受益。类似以下内容可能会起作用:
SELECT CASE WHEN EXISTS(SELECT 1
FROM inv_y
WHERE (ip IN (SELECT identifier
FROM inv_x)
OR hostname IN (SELECT identifier
FROM inv_x))
AND hostname LIKE '%eb%'
) = 1 THEN "True"
ELSE "False"
END AS Present;
这是此工作的演示:SQL Fiddle
请注意,如果可以使用1或0(而不是true或false),则可以进一步简化上述内容。 EXISTS
如果找到该行,则返回1,否则返回0,并且CASE
在那里只是将其转换为'True'或'False',因此您可以简单地进行以下操作:< / p>
SELECT EXISTS(SELECT 1
FROM inv_y
WHERE (ip IN (SELECT identifier
FROM inv_x)
OR hostname IN (SELECT identifier
FROM inv_x)
)
AND hostname LIKE '%eb%') AS Present;