即使一个表中没有数据,也可以从多个表中选择数据 [英] Select data from multiple tables even if there no data in one table
问题描述
你好
即使没有其他表的数据,我怎么也能从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
推荐答案
如果另一个表中没有匹配的记录,请使用外部联接获取记录.请参阅: http://msdn.microsoft.com/en-us/library/ms187518.aspx [ ^ ]
因此,在您的声明中,类似以下内容:
Use outer join to get records if there''s no matching record in another table. See: http://msdn.microsoft.com/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.
这篇关于即使一个表中没有数据,也可以从多个表中选择数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!