3

How do I use SELECT syntax inside WHERE for example I have following code:

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

Here I am comparing values if its greater than or equal to 1908932600. I want to use SELECT in place of 1908932600 so it will automatically compare values rather than statically code in query.

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')

Muti Value サブクエリ

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 に答える