使用oracle从两个表中选择

时间:2013-10-08 14:53:28

标签: sql oracle

我有两个表,即历史记录和错误列表。 历史记录包含具有详细信息的事务记录。其中之一是错误代码。即错误列表表包含错误代码和描述的列表。现在我想从两个表中选择结果,显示历史表中出现不同错误代码的次数以及错误列表表中相同错误代码的相关错误描述。请帮忙。

1 个答案:

答案 0 :(得分:0)

假设您想要在两个表上进行内部联接:

select errorcode, errordescription, count(*)
from error, history
where history.errorcode = error.code
group by history.errorcode, history.errordescription

编辑:

假设错误代码在错误表上是唯一的,并使用您提供的字段名称:

select h.errorcode, count(*) as count
from history h
group by h.errorcode

如果您还需要描述,则可能需要包含子查询:

select z.errorcode, (select errordesc from error where errorcode = z.errorcode), z.count
from (
 select h.errorcode, count(*) as count
 from history h
 group by h.errorcode
) z
相关问题