我想写一个基于Oracle的查询,我可以选择是否要查看结果。让我们说:
SELECT *
FROM table
//when there are more than 10 rows
我该怎么做?
答案 0 :(得分:15)
select * from table
where 10 < (select count(*) from table)
答案 1 :(得分:8)
最佳速度:
select * from table
where 10=(select count(*) from
table
where rownum <11)
:)
更新:因为怀疑我声称某些事情不属实,所以在这里进行一些测试: 在SQL Developer中(请记住,select * from table将只提供前50行,但count(*)读取所有请求的行) 该表没有索引。
select
count(*) from
table
22074412行
3.16秒
select * from table where 10 =
(select
count(*) from
table
where rownum <11
)
0.051秒
select * from table where 10 <
(select
count(*) from
table
)
3.39秒
select count(*) from table where 10 <
(select
count(*) from
table
)
7.69秒
select count(*) from table where 10 =
(select
count(*) from
table
where rownum <11
)
3.42秒
原因:带有rownum的子查询更快(它不是整个表)
答案 2 :(得分:2)
DECLARE @Var int;
SET @Var = SELECT COUNT(*) FROM [somewhere]
IF @Var > 10
BEGIN
SELECT * FROM [somewhere]
END
你的意思是那样的? 或者只是如何使用where子句?
SELECT *
FROM [somewhere]
WHERE (SELECT COUNT(*) FROM [somewhere]) > 10
答案 3 :(得分:1)
这应该适合你:
SELECT * FROM [table] WHERE (SELECT COUNT(1) FROM [table]) > 10
答案 4 :(得分:1)
select * from YourTable where (select count(*) from YourTable ) > 10
答案 5 :(得分:1)
如果您想避免双重扫描并且您有可用的有效统计信息
select * from table a, all_tables b
where b.num_rows > 10
and b.table_name = 'table';