一个关于数据查询的难题,高手来啊

starj1 2005-10-11 10:05:52
有二个表,其中A表的二个列要求关联到另B表的同一列。查询的结果要求按A表的二列显示出B表的二列内容。
具体如下:
表A:
列1:father As Int32 '父亲的代码
列2:child As Int32 '儿子的代码
列3:mood As Int32 ’感情程度表示
表B:
列1:code As Int 32 '人物代码
列2:personename As String '人名

要求查询显示出下结果:
父亲的名字,儿子的名字,感情程度

有高手在赶紧指点我下啊~~~~~~~~~
...全文
159 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
starj1 2005-10-13
  • 打赏
  • 举报
回复
gyf19(秋天的云)和 flygoldfish(长江支流) 的方法可行,
jinjazz(近身剪(充电中...)) 的SQL在OLEDb不支持,没法试。。。
hdt(近来工作忙,每天只能接分半个小时) 你也是用表的别名,应该可以,没去试^_^

结贴。
gyf19 2005-10-12
  • 打赏
  • 举报
回复
联表查询!!
select (select personename form B where code = A.father ) AS fatherName , (select personename form B where code = A.child ) AS childName ,mood form A

自己想的,不知道行不行!!
gyf19 2005-10-12
  • 打赏
  • 举报
回复
强,学习中.......见笑了
jinjazz 2005-10-12
  • 打赏
  • 举报
回复
--建立测试环境
Create Table a(father varchar(10),child varchar(10),mood varchar(10))
--插入数据
insert into a
select '1','2','100' union
select '1','3','90' union
select '2','4','60' union
select '2','5','70'

Create Table b(code varchar(10),personename varchar(10))
--插入数据
insert into b
select '1','aa' union
select '2','bb' union
select '3','cc' union
select '4','dd' union
select '5','ee'

--测试语句
select b1.personename as father,b2.personename as child,a.mood from a,b b1,b b2
where a.father=b1.code and a.child=b2.code



--删除测试环境
Drop Table a
Drop Table b

/*--测试结果
father child mood
---------- ---------- ----------
aa bb 100
aa cc 90
bb dd 60
bb ee 70
---*/
长江支流 2005-10-12
  • 打赏
  • 举报
回复
楼主用
Select personename As 父亲的名字,
(Select tmp.child 儿子的名字 From A tmp Where tmp.father=A.father) As 儿子的名字,
mood As 感情程度
From A Inner Join B On A.father=B.code

即可解决问题
如果楼主不想用T_SQL的写法而用书上的写法
From A Inner Join B On A.father=B.code
改成
From A,B Where A.father=B.code


真相重于对错 2005-10-12
  • 打赏
  • 举报
回复
select bp.name , bc.name , a.mode from a
join b as bp on bp.code = a.father
join b as bc on bc.code = a.child
长江支流 2005-10-12
  • 打赏
  • 举报
回复
可以这样

这是单独立列出来的,personename 与 child 没有关系
Select personename As 父亲的名字,mood As 感情程度 From A Inner Join B On A.father=B.code

Union

Select child 儿子的名字,mood As 感情程度 From A Inner Join B On A.child =B.code

如果要有关系,如下(子查询):

Select personename As 父亲的名字,

(
Select tmp.child 儿子的名字 From A tmp Where tmp.father=A.father
)

,mood As 感情程度 From A Inner Join B On A.father=B.code


DataTable、DataView、DataGrid各列数据查询和DataGrid、报表打印源码
www.webmis.com.cn


长江支流 2005-10-12
  • 打赏
  • 举报
回复
楼主理解一下表用别名就可以了,可以把A表当成两个表
aimcy 2005-10-12
  • 打赏
  • 举报
回复
select a.mood mood,b.personname fathername,c.personname childname
from tableA a,tableB b,tableB c where a.father=b.code and a.child=c.code
starj1 2005-10-12
  • 打赏
  • 举报
回复
先试下再回来请教,看得头有点晕:(
starj1 2005-10-11
  • 打赏
  • 举报
回复
只是这样的意思,表A存储的是父亲和儿子的人物代码和感情程度,表B存储各个人物的名字。现在需要显示出有父子关系的人物的感情程度。
这样的join不知道怎么写。。。
starj1 2005-10-11
  • 打赏
  • 举报
回复
xwqjamky(陌上烟花) ,你没理解我的意思,表的关系是这样的。

表A 表B

father As Int32 '父亲的代码─────────列1:code As Int 32 '人物代码
                  │
列2:child As Int32 '儿子的代码───┘ 列2:personename As String '人名
列3:mood As Int32 ’感情程度表示
xwqjamky 2005-10-11
  • 打赏
  • 举报
回复
表A应差人物代码吧

Table A
father ,child, mood ,code

Table B
code 字段应与 Table A ,code字段 foreign key Table A (code)

16,553

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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