【问题标题】:Simplify sql query for getting single data简化 sql 查询以获取单个数据
【发布时间】:2013-07-26 10:31:07
【问题描述】:

首先,我的数据库结构如下所示

Table Application:ID(pk), App_num, App_name, App_owner, App_sec
Table App_runner: ID(pk), app_id(FK:APP.id), runner_name
Table Jar_used:   ID(pk), runId(FK: App_runner.id),jar_name, time_stamp
Table Jar_static: ID(pk), jar_name, current_version, version_status(either of C,S,D)

我需要的结果集如下:

 App_Num, App_Name, App_Owner, Compliance

其中Compliance 是基于Jar_static 表中version_status 的派生列,“C”=当前,“S”=支持,“D”=已弃用。

结果集条件还有:

app.id=app_runner.app_id
app_runner.id=jar_used.runId
jar_used.jar_name=jar_static.jar_name

以下查询对我来说很好:

select app.APP_NUM, app.APP_NAME ,app.APP_OWNER , case jars.version_status 
 when 'C' then 'CURRENT' 
 when 'S' then 'SUPPORTED' 
 else 'DEPRECATED' 
end as COMPLIANCE 
from Application as app, app_runner as runner, jar_used as used, jar_static as jars 
    where app.id = runner.app_id and 
    runner.run_id = used.app_run_id and 
    jars.jar_name =used.jar_name

有人可以建议一种简化的方法吗?
还有一个可能的要求是找到jar_used.jar_name='abc' 以及其他三个条件的记录。再添加一个可以给出结果,但我正在寻找一些简化的方法。 谢谢你的帮助。

【问题讨论】:

  • 有些白痴否决了我的问题。惊讶!!!

标签: sql database oracle join


【解决方案1】:

您的查询很好。您需要浏览所有四个表以获取所需的信息。查询不是特别复杂。

不过,请注意。您应该学习正确的连接语法。它使查询更易于阅读、功能更强大,并有助于防止错误:

select app.APP_NUM, app.APP_NAME ,app.APP_OWNER ,
       (case jars.version_status 
          when 'C' then 'CURRENT' 
          when 'S' then 'SUPPORTED' 
          else 'DEPRECATED' 
        end) as COMPLIANCE 
from Application as app join
     app_runner as runner
     on app.id = runner.app_id join
     jar_used as used
     on runner.run_id = used.app_run_id join
     jar_static as jars 
     on jars.jar_name =used.jar_name;

编辑:

对特定 jar 使用此查询的正确方法是添加 where 子句:

select app.APP_NUM, app.APP_NAME ,app.APP_OWNER ,
       (case jars.version_status 
          when 'C' then 'CURRENT' 
          when 'S' then 'SUPPORTED' 
          else 'DEPRECATED' 
        end) as COMPLIANCE 
from Application as app join
     app_runner as runner
     on app.id = runner.app_id join
     jar_used as used
     on runner.run_id = used.app_run_id join
     jar_static as jars 
     on jars.jar_name =used.jar_name
where jar_name = 'abc'

如果查询对您来说很复杂,您可以将其放入视图中:

create view vw_application_jar as 
select app.APP_NUM, app.APP_NAME ,app.APP_OWNER ,
       (case jars.version_status 
          when 'C' then 'CURRENT' 
          when 'S' then 'SUPPORTED' 
          else 'DEPRECATED' 
        end) as COMPLIANCE,
       jar_name
from Application as app join
     app_runner as runner
     on app.id = runner.app_id join
     jar_used as used
     on runner.run_id = used.app_run_id join
     jar_static as jars 
     on jars.jar_name =used.jar_name

然后你可以使用:

select *
from vw_application_jar 
where jar_name = 'abc';

【讨论】:

  • 感谢您的帮助。但是,您能否在最后一个条件下提出一些建议。除了在 jar_name='abc' 的位置再添加一个“和”,还有其他方法吗?
  • 您的查询与我的查询有帮助并且工作方式相同。谢谢你。您能否建议我如何在同一查询中根据 runner.app_id 从 ap_runner 获取最新信息。如果有多个记录,我的要求已更改为获取最新记录,基于最新的 app_runner。感谢您的帮助。
猜你喜欢
  • 2018-09-06
  • 2021-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多