多条数据查询

more than one data query

我的 oracle 数据库中有一个历史 table。

我只想select特定ID的多条记录。

Select * from user_history

Table结构:

ID History_Text
1  xxxxx
1  yyyyy
2  zzzzz  

table 上有数百万条记录。而且我不知道哪个ID有历史。

例如:我只想select有历史,例如ID=1的记录。

我该怎么做?

只需使用 where 子句。像这样:-

Select * 
from user_history
having count(id) = 2;

问题不是很清楚。它要么非常基础,要么有点复杂。

如果你想 select 行有 id = 1 然后使用 where 子句

Select * from user_history where id = 1 

如果您只想 select 那些 ID 有多个记录的记录,那么使用计数函数

select ID, History_Text from 
(Select ID, History_Text,count(*) over(partition by ID) cnt from user_history) t 
where cnt > 1

这是一个简单的 aggregation/counting,对聚合值有条件:

select id
from user_history
group by id
having count(*) > 1;