[英]query with left outer join in sql
I have an issue with the below query:我对以下查询有疑问:
select main.courseid
,main.coursename
,main.catid
,main.catname
,main.need_dte
from (
select t1.courseid
,t1.coursename
,t2.catid
,t2
,catname
,t2.need_dte
from t1
,t2
where t1.courseid = t2.courseid
and t1.coursename = t2.coursename
) main
left outer join (
select courseid
,coursename
,need_dte training_info
) ui on main.courseid = ui.courseid
and main.coursename = ui.coursename
and main.need_dte = ui.need_dte
I have the above scenario in which i am trying to do left outer join between the tables "main" and "training_info".我有上面的场景,我试图在表“main”和“training_info”之间做左外连接。
main table: a inner join between t1 and t2 to get the training and the category details.主表:t1 和 t2 之间的内部连接,用于获取训练和类别详细信息。
training_info(ui): has training details without category details. training_info(ui):有没有类别细节的训练细节。
here i have few course details in "main" and "ui" tables in common.在这里,我在“main”和“ui”表中几乎没有共同的课程细节。 and i have few unique course records in "main" table not in "ui" table.
而且我在“主”表中没有在“ui”表中的独特课程记录很少。 so i am trying to extract both the unique and common records.
所以我试图提取唯一和常见的记录。
I am able to get the results for this join, but the issue is with the need_dte.我能够获得此连接的结果,但问题出在 need_dte 上。 the need_dte field is present in both tables.In the result if the records are from "main" table am able to get the need_dte field updated from the inner table t2.
两个表中都存在need_dte 字段。结果,如果记录来自“主”表,则可以从内部表t2 中更新need_dte 字段。 if the records are from "ui" table in the result, the need_dte is not being populated.
如果记录来自结果中的“ui”表,则不会填充 need_dte。
Is there any way using this join set up I need to get the need_dte for the result records from training_info table also if those records have a need_dte.有什么方法可以使用此连接设置,如果这些记录有need_dte,我还需要从training_info 表中获取结果记录的need_dte。
Thanks!谢谢!
Is this what you want?这是你想要的吗?
select t1.courseid, t1.coursename, t2.catid, t2.catname,
coalesce(ti.need_dte, t2.need_dte ) as need_dte
from t1 join
t2
on t1.courseid = t2.courseid and
t1.coursename = t2.coursename left outer join
training_info ti
on t1.courseid = ti.courseid and
t1.coursename = ti.coursename and
t2.need_dte = ti.need_dte;
I find your query with the nested subqueries, multiple levels of naming, and archaic join
syntax to be difficult to read.我发现您的带有嵌套子查询、多级命名和陈旧
join
语法的查询难以阅读。 I think the above is what you are looking for.我认为以上就是你要找的。 It returns
need_dte
from training_info
, if present, and then from t2
.它从
training_info
返回need_dte
(如果存在),然后从t2
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.