sqlserver2005 多表数据库查询

wl_ldy 2010-02-04 04:00:57
问题是这样的:
有三个数据库表:

表a 表b 表c

id id id
b_id name b_id
message message

想查询的结果是 :从a表和b表中 查询 a.id , b_id , name ,a.message (1)
或从b表和c表中 查询 c.id , b_id ,name ,c.message (2)
把(1)和 (2)中数据全部查出来 放到一个DataSet集合中,
从网上也查到 要用 视图 、存储过程之类的 ,但是刚接触 还是不太会 ,
希望高手指点。。。
先谢过了。。。
...全文
182 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
wl_ldy 2010-02-04
  • 打赏
  • 举报
回复
恩 受益了 马上结贴,谢谢各位高人指点。。。
fwacky 2010-02-04
  • 打赏
  • 举报
回复
create view Test
as
select a.id as mid,a.b_id as mbid,a.message as mmessage,b.name as mname from a,b where a.id=b.id
union all
select c.id as mid,c.b_id as mbid,c.message as mmessage,b.name as mname from b,c where b.id=c.id

直接
select * from Test

绑定视图和表是一样的!
wl_ldy 2010-02-04
  • 打赏
  • 举报
回复
依照 2楼和4楼的方法 现在 问题解决了 ,
谢谢各位的 帮助 !另外想问一下 要创建视图 是这样吗?

create view Test
as
select a.id as mid,a.b_id as mbid,a.message as mmessage,b.name as mname from a,b where a.id=b.id
union all
select c.id as mid,c.b_id as mbid,c.message as mmessage,b.name as mname from b,c where b.id=c.id


要是这样的 话那在 Gridview中如何绑定视图呢?
lmn788209 2010-02-04
  • 打赏
  • 举报
回复
这里的高手还是挺多的 受益了
flyerwing 2010-02-04
  • 打赏
  • 举报
回复
select
(select name from bbb where bbb.id=aaa.id)
contents
from aaa
估计行吧
chengjianhuadi 2010-02-04
  • 打赏
  • 举报
回复
union 有三种格式
像楼上的是取出量表的所有乘积
还有的是
select a.idas mid,a.b_idas mbid,a.messageas mmessage,b.nameas mnamefrom a,b where a.id=b.id
union left
select c.idas mid,c.b_idas mbid,c.messageas mmessage,b.nameas mnamefrom b,cwhere b.id=c.id
这个是取出第一个表的所有。
select a.idas mid,a.b_idas mbid,a.messageas mmessage,b.nameas mnamefrom a,b where a.id=b.id
union right
select c.idas mid,c.b_idas mbid,c.messageas mmessage,b.nameas mnamefrom b,cwhere b.id=c.id
这个是取出第2个表的所有。
ZHUKY 2010-02-04
  • 打赏
  • 举报
回复
SQL codeselect a.idas mid,a.b_idas mbid,a.messageas mmessage,b.nameas mnamefrom a,bwhere a.id=b.idunionallselect c.idas mid,c.b_idas mbid,c.messageas mmessage,b.nameas mnamefrom b,cwhere b.id=c.id

Up ↑
wl_ldy 2010-02-04
  • 打赏
  • 举报
回复
哦 好的 先试试了 谢过了。。。
jenny0810 2010-02-04
  • 打赏
  • 举报
回复

select a.id as mid,a.b_id as mbid,a.message as mmessage,b.name as mname from a,b where a.id=b.id
union all
select c.id as mid,c.b_id as mbid,c.message as mmessage,b.name as mname from b,c where b.id=c.id
这样试试
criedshy 2010-02-04
  • 打赏
  • 举报
回复
查出来的数据集可以直接填充DataSet 此时的DataSet是两个DataTable的集合
criedshy 2010-02-04
  • 打赏
  • 举报
回复
select a.id,b.b_id,b.name,a.message from 表a as a inner join 表b as b on a.b_id=b.id

select c.id,b.b_id,b.name,c.message from 表c as c inner join 表b as b on c.b_id=b.id
jenny0810 2010-02-04
  • 打赏
  • 举报
回复

(1)select a.*,b.* from a,b where a.id=b.id
(2)select b.*,c.* from b,c where b.id=c.id

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧