3

如何SELECT在内部使用语法WHERE,例如我有以下代码:

SELECT 
    blah...blah..
    ...
WHERE  
(
  (APM_AlertsAndReportsData.ApplicationName = 'instance: tomcat6_noram (SNMP)') AND 
  (APM_AlertsAndReportsData.ComponentName = 'Memory Heap Used (B)') AND 
  (APM_AlertsAndReportsData.StatisticData >= 1908932600)
)

在这里,我正在比较大于或等于的值1908932600。我想使用它SELECT来代替,1908932600所以它会自动比较值而不是查询中的静态代码。

4

3 回答 3

5

您可以使用子查询,只需要在它周围加上括号:

APM_AlertsAndReportsData.StatisticData >= (SELECT ... FROM ...)

请注意,它可能运行缓慢,具体取决于您在子查询中执行的操作。改用 JOIN 可能会更好。

于 2012-04-27T15:37:20.597 回答
3

您需要 WHERE 子句中的“相关子查询”:

WHERE  
(
  (APM_AlertsAndReportsData.ApplicationName = 'instance: tomcat6_noram (SNMP)') AND 
  (APM_AlertsAndReportsData.ComponentName = 'Memory Heap Used (B)') AND 
  (APM_AlertsAndReportsData.StatisticData >= (SELECT SomeValue FROM SomeTable))
)
于 2012-04-27T15:37:59.937 回答
2

这是一个简单的:

单值子查询

select * from Table1
where id = (select id from Table2 where Name = 'cool')

多值子查询

select * from Table1
where id IN (select id from Table2 where Name LIKE 'A%')

select * from Table1
where id NOT IN (select id from Table2 where Name LIKE 'A%')

相关子查询

select * from Table1
where exists (select 1 from Table2 where Table1.id = Table2.id)
于 2012-04-27T15:41:49.310 回答