您使用什么集合来接受来自 oracle 的 select 1 的 return 值?

what collection do you use to accept the return value of select 1 from oracle?

我有一个 oracle table,我想检查是否存在。

Select 1 from table where match_id = 'xxxx' 会 return 0 行、1 行或多行 1,具体取决于有多少匹配项。

我是用myBatis写的sql,那么我应该用什么样的集合来接受结果呢?

一种方法是通过从双重

中选择来使用EXISTS
SELECT
     CASE
          WHEN EXISTS (
               SELECT 1
               FROM tablename
               WHERE match_id = 'xxxx'
          ) THEN 1                  --exists
          ELSE 0                    --does not exists
     END
FROM dual;

或者使用ROWNUM = 1限制结果,然后计算

select count(*) from 
(
  Select 1 from tablename where match_id = 'xxxx' and rownum = 1
);

两者应该具有相似的性能,并且比在整个 table 上执行 select count(*) 更好。