Oracle 将以下查询作为一个查询联接是否可行
能否将以下查询合并到单个查询中 一, 二, 三, 四,Oracle 将以下查询作为一个查询联接是否可行,oracle,oracle11g,Oracle,Oracle11g,能否将以下查询合并到单个查询中 一, 二, 三, 四, 如果不知道您的表结构,就很难准确地说出哪种方式会给您带来最好的结果。您可以将查询重写为一个查询,将不同的表连接在一起,但是您从中选择的表似乎完全不同。如果不知道你的桌子设计,很难说这是不是最好的做法 或者,如果您只是希望将结果集合并到单个数据集中,基本上将每个查询的结果合并到单个输出中,那么您可以使用UNION[ALL]。您需要确保每个查询的select列表中的列正确匹配/对齐。这并不理想,但它应该能满足你的需求 with combined
如果不知道您的表结构,就很难准确地说出哪种方式会给您带来最好的结果。您可以将查询重写为一个查询,将不同的表连接在一起,但是您从中选择的表似乎完全不同。如果不知道你的桌子设计,很难说这是不是最好的做法 或者,如果您只是希望将结果集合并到单个数据集中,基本上将每个查询的结果合并到单个输出中,那么您可以使用
UNION[ALL]
。您需要确保每个查询的select列表中的列正确匹配/对齐。这并不理想,但它应该能满足你的需求
with combined_results as (
select a.usr_id as usr_id,
a.usr_desc as usr_desc,
a.usr_type as usr_type,
b.usr_desc as usr_prof_name,
null as dept_name,
null as cur_code,
0 as min_amt,
0 as max_amt,
null as msg_type
from sc_usr_prof_m a, sc_usr_type_m b
where a.usr_type=b.usr_type
and a.current_status='A'
union all
select distinct
a.usr_id as usr_id,
a.usr_desc as usr_desc,
null as usr_type,
null as usr_prof_name,
b.dept_name as dept_name,
null as cur_code,
0 as min_amt,
0 as max_amt,
null as msg_type
from sc_usr_prof_m a ,sc_user_depts b
where a.usr_id=b.usr_id
and a.usr_status='E'
and a.current_status='A'
union all
select a.usr_id as usr_id,
a.usr_desc as usr_desc,
null as usr_type,
null as usr_prof_name,
null as dept_name,
b.curr_code as curr_code,
b.min_amt as min_amt,
b.max_amt as max_amt,
null as msg_type
from sc_usr_prof_m a, sc_auth_limit_m b
where a.usr_id=b.usr_id
and a.usr_status='E'
and a.current_status='A'
and b.max_amt not in ('0')
union all
select a.usr_id as usr_id,
a.usr_desc as usr_desc,
null as usr_type,
null as usr_prof_name,
null as dept_name,
null as cur_code,
0 as min_amt,
0 as max_amt,
b.msg_type as msg_type
from sc_usr_prof_m a, sc_user_msgs_m b
where a.usr_id=b.usr_id
and a.usr_status='E'
and a.current_status='A'
and b.swift_enable='Y'
)
select *
from combined_results
order by usr_id;
我还建议您写下您的加入:
select a.col1, a.col2, b.col3, b.col4
from my_a_table a
join my_b_table b
on b.col1 = a.col1
where a.col1 = 123;
而不是您当前的操作方式:
select a.col1, a.col2, b.col3, b.col4
from my_a_table a, my_b_table b
where b.col1 = a.col1
and a.col1 = 123;
如果不是出于可读性的原因,尤其是在具有多个联接的较大查询中。欢迎使用SO,我对您的查询进行了格式化,并根据您的标题在正文中添加了一个简单的问题,但您应该在实际的帖子中添加更多内容。如果您给出一个表结构示例、一些示例数据和一个您希望结果数据集看起来像什么的示例,这也会有所帮助。请花点时间通读这一部分。可行的,是的。明智的,也许不是。第一个查询从
sc_usr_prof_m
中过滤一组不同的记录,因此将其与其他查询组合会更改结果集。每个查询都连接到不同的表,这些表除了usr\u id
之外可能没有公共的连接列,因此将生成笛卡尔乘积。请帮我解决这个问题
select a.usr_id,a.usr_desc,b.msg_type
from sc_usr_prof_m a, sc_user_msgs_m b
where a.usr_id=b.usr_id
and a.usr_status='E'
and a.current_status='A'
and b.swift_enable='Y'
order by a.usr_id;
with combined_results as (
select a.usr_id as usr_id,
a.usr_desc as usr_desc,
a.usr_type as usr_type,
b.usr_desc as usr_prof_name,
null as dept_name,
null as cur_code,
0 as min_amt,
0 as max_amt,
null as msg_type
from sc_usr_prof_m a, sc_usr_type_m b
where a.usr_type=b.usr_type
and a.current_status='A'
union all
select distinct
a.usr_id as usr_id,
a.usr_desc as usr_desc,
null as usr_type,
null as usr_prof_name,
b.dept_name as dept_name,
null as cur_code,
0 as min_amt,
0 as max_amt,
null as msg_type
from sc_usr_prof_m a ,sc_user_depts b
where a.usr_id=b.usr_id
and a.usr_status='E'
and a.current_status='A'
union all
select a.usr_id as usr_id,
a.usr_desc as usr_desc,
null as usr_type,
null as usr_prof_name,
null as dept_name,
b.curr_code as curr_code,
b.min_amt as min_amt,
b.max_amt as max_amt,
null as msg_type
from sc_usr_prof_m a, sc_auth_limit_m b
where a.usr_id=b.usr_id
and a.usr_status='E'
and a.current_status='A'
and b.max_amt not in ('0')
union all
select a.usr_id as usr_id,
a.usr_desc as usr_desc,
null as usr_type,
null as usr_prof_name,
null as dept_name,
null as cur_code,
0 as min_amt,
0 as max_amt,
b.msg_type as msg_type
from sc_usr_prof_m a, sc_user_msgs_m b
where a.usr_id=b.usr_id
and a.usr_status='E'
and a.current_status='A'
and b.swift_enable='Y'
)
select *
from combined_results
order by usr_id;
select a.col1, a.col2, b.col3, b.col4
from my_a_table a
join my_b_table b
on b.col1 = a.col1
where a.col1 = 123;
select a.col1, a.col2, b.col3, b.col4
from my_a_table a, my_b_table b
where b.col1 = a.col1
and a.col1 = 123;