即使一个表中没有数据,也可以从多个表中选择数据

编程入门 行业动态 更新时间:2024-10-09 12:24:33
本文介绍了即使一个表中没有数据,也可以从多个表中选择数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

你好 即使没有其他表的数据,我怎么也能从2个表中获取数据 例如:fathers表具有column_id,name列.sons表具有column_id,name列 此查询 从父亲f,儿子​​s中选择f.father_id,f.name,s.name 其中f.father_id = s.father_id和father_id = @ parameter 我想从这些表中获取数据,如果存在与父表相关的father_id,并且不作为空或任何空字符串 如果子表中没有数据,就这样 父亲编号,父亲姓名,儿子姓名 1个templ null 2约翰·空 感谢all

hello how can i get data from 2 tables even if there is no data into other table ex: fathers table has columns father_id,name .. sons table has columns son_id ,name this query select f.father_id,f.name,s.name from fathers f,sons s where f.father_id=s.father_id and father_id=@parameter i want to get data from these tables if exists related father_id into son table and if not get it as null or any empty string like this if no data into son table father_id,fathername ,sonname 1 templ null 2 john null thanks all

推荐答案

如果另一个表中没有匹配的记录,请使用外部联接获取记录.请参阅: msdn.microsoft/en-us/library/ms187518.aspx [ ^ ] 因此,在您的声明中,类似以下内容: Use outer join to get records if there''s no matching record in another table. See: msdn.microsoft/en-us/library/ms187518.aspx[^] So in you statement, something like: select f.father_id,f.name,s.name from fathers f left outer join sons s on f.father_id=s.father_id

如果要限制使用father_id,则类似:

If you want to restrict using father_id, something like:

SELECT f.father_id, f.name, s.name FROM fathers f LEFT OUTER JOIN sons s ON f.father_id = s.father_id WHERE f.father_id = @father_id

如果有的话,这应该带来一个全子的单亲父亲.

That should bring a single father with all sons, if any exist

使用左外部联接建立您的查询. Use left outer join to build your query.

更多推荐

即使一个表中没有数据,也可以从多个表中选择数据

本文发布于:2023-11-28 01:03:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1640349.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   多个

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!