首先,我的数据库结构如下所示
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_status
表的Jar_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'
以及其他三个条件的记录。添加一个,可以给出结果,但我正在寻找一些简化的方法。
谢谢你的帮助。
答案 0 :(得分: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';