怎样把三个表的字段用一条SQL语句查出来?
数据库中有这样的记录
table1
callno username companyid biggroupid smallgroupid
13760705651 test1 111111 11 1
13760705652 test2 111111 22 1
13760705653 test3 111111 11 2
13760705654 test7 111112 11 1
13760705655 test8 111112 11 2
table2
biggroupid companyid biggroupname
11 111111 大组一
12 111111 大组二
22 111111 大组三
33 222222 大组四
table3
smallgroupid biggroupid smallgroupname
1 11 小组一
2 11 小组二
3 22 22大组一小组
查询CompanyID为111111时显示的记录是这样子的
callno usernam biggroupname smallgroupname
13760705651 test1 大组一 小组一
13760705652 test2 大组二 22大组一小组
13760705653 test3 大组一 小组二
问题点数:50、回复次数:8Top
1 楼paoluo(一天到晚游泳的鱼)回复于 2006-06-03 15:06:37 得分 25
Select
A.callno,
A.usernam,
B.biggroupname,
C.smallgroupname
From table1 A
Inner Join table2
On A.biggroupid=B.biggroupid
Inner Join table3
On A.smallgroupid=B.smallgroupidTop
2 楼wwh999(印钞机V2.0...开发中....)回复于 2006-06-03 15:08:10 得分 15
select
A.callno,A.username,B.biggroupname,C.smallgroupname
from table1 A,table2 B,table3 C
where
A.companyid=B.companyid And A.biggroupid=B.biggroupid
and A.smallgroupid=C.smallgroupname
order by A.callnoTop
3 楼paoluo(一天到晚游泳的鱼)回复于 2006-06-03 15:08:24 得分 0
上面有錯誤,改
Select
A.callno,
A.usernam,
B.biggroupname,
C.smallgroupname
From table1 A
Inner Join table2 B
On A.biggroupid=B.biggroupid
Inner Join table3 C
On A.smallgroupid=C.smallgroupid
Where A.companyid='111111'
Or
Select
A.callno,
A.usernam,
B.biggroupname,
C.smallgroupname
From table1 A,table2 B,table3 C
Where A.biggroupid=B.biggroupid And A.smallgroupid=C.smallgroupid And A.companyid='111111'Top
4 楼wwh999(印钞机V2.0...开发中....)回复于 2006-06-03 15:13:06 得分 0
上面太多东东,不知道有没有看花眼,或者有打错字母的也不一定..呵呵~
可以有两种写法:
1>from 多表+where中条件限制
2>使用内联接inner join 表连接,在on中设置表连接条件
在2楼的贴子中答的是第一种方法,此为第2种
select
A.callno,A.username,B.biggroupname,C.smallgroupname
from table1 A inner join table2 B
on A.biggroupid=B.biggroupid inner join table3 C
on A.smallgroupid=C.smallgroupid
Top
5 楼itblog(Just for wife!)回复于 2006-06-03 15:13:35 得分 10
select * from
table1 t1
left join table2 t2 on t1.biggroupid=t2.biggroupid and t1.companyid=t2.companyid
left join table3 t3 on t1.smallgroupid=t3.smallgroupid and t1.biggroupid=t3.biggroupid
where t1.companyid='111111'Top
6 楼patann()回复于 2006-06-03 15:23:31 得分 0
谢谢....
oracle不支持 Inner 这些关健字的吗Top
7 楼paoluo(一天到晚游泳的鱼)回复于 2006-06-03 15:33:33 得分 0
是,你用我寫的第二種方法。Top
8 楼patann()回复于 2006-06-03 15:39:36 得分 0
谢谢....Top




