问题描述
我需要有关SQL查询的帮助,无论条件是否存在,我都希望在其中获取数据。
select employee.*,stream.*,priority.*
from employee emp,stream st,priority pr
where emp.st.id = st.id
and str.pr.id = pr.id
因此,此处的优先级不是强制性的。一个值可能存在或可能不存在。因此,如果不存在优先级,则应为emp.st.id = st.id提取数据。如果存在,则应包括emp.pr.id = pr.id。
预先感谢
解决方法
这就是你所说的:
select employee.*,stream.*,priority.* from employee emp,stream st,priority pr --> priority is not mandatory where emp.st.id = st.id and str.pr.id = pr.id
翻译从英语到SQL,它读作“外部连接”,这意味着这样的事情可能会满足您的要求:
select e.*,s.*
p.*
from employee e join stream s on s.id = e.st_id
left join priority p on p.id = s.pr_id --> this
,
希望以下建议对您有帮助
select employee.*,priority pr
where emp.st.id = st.id
and str.pr.id = pr.id(+)
有关左/右外部连接的更多详细信息,可以检查以下链接。 https://docs.oracle.com/cd/B28359_01/server.111/b28286/queries006.htm#SQLRF52331