-1

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

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是基于表的派​​生列version_statusJar_static“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'与其他三个条件一起出现的记录。再添加一个可以给出结果,但我正在寻找一些简化的方法。谢谢你的帮助。

4

1 回答 1

2

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

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

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';
于 2013-07-26T10:57:35.770 回答