使用oracle从两个表中选择

时间:2021-03-10 15:38:56

I have two tables namely history and errorlist. History contains records of a transaction with details. One of these is the errorcode. Namely errorlist table contains list of error codes and description. Now i want to select the result from two tables showing the number of times a distinct error code occurred from the history table and the relevant error description for the same errorcode from errorlist table. Please help.

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

1 个解决方案

#1


0  

Assuming you want an inner join on the two tables:

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

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

EDIT:

Assuming the error code is unique on the error table, and using the field names that you've provided:

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

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

If you need the description, too then you may need to include a subquery:

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

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

#1


0  

Assuming you want an inner join on the two tables:

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

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

EDIT:

Assuming the error code is unique on the error table, and using the field names that you've provided:

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

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

If you need the description, too then you may need to include a subquery:

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

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