一个简单的sql 语句查询问题。

yangfeng1819 2011-07-23 09:17:46
有一张公司详细信息表。外键有地区id和行业id 地区是通过父id关联起来的。 如安徽省-合肥市-肥东县 怎么才能查询出所有安徽省的公司详细信息。
...全文
161 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
cd731107 2011-07-26
  • 打赏
  • 举报
回复


create table bookArea(area_id int,book_parentid int,book_area nvarchar(50))
insert bookArea
select 1,0,N'所有地区' union all
select 2,1,N'安徽省' union all
select 3,2,N'合肥市' union all
select 4,3,N'长丰县' union all
select 5,3,N'肥东县' union all
select 6,3,N'肥西县'
go

create function f_cid(@ID int) returns @t_level table(area_id int,book_area nvarchar(50)
, level int)
as
begin
declare @level int
declare @area nvarchar(50)
set @level = 1
select @area=book_area from bookArea where area_id=@ID --加上这句赋值
insert into @t_level select @id,@area,@level --原来@area未赋值,所以是null
while @@ROWCOUNT > 0
begin
set @level = @level + 1
insert into @t_level select a.area_id ,a.book_area, @level
from bookArea a , @t_Level b
where a.book_parentid = b.area_id and b.level = @level - 1
end
return
end

go
select * from f_cid(2)

/*
area_id book_area level
----------- -------------------------------------------------- -----------
2 安徽省 1
3 合肥市 2
4 长丰县 3
5 肥东县 3
6 肥西县 3

(5 行受影响)
*/
cd731107 2011-07-23
  • 打赏
  • 举报
回复
select * from 公司详细信息表
where areaid
in (
select a.areaid from 地区表 a, 地区表 b
where a.parentid=b.areaid and b.area='安徽省'
union all
select a.areaid from 地区表 a, 地区表 b,地区表 c
where a.parentid=b.areaid and b.parentid=c.areaid and c.area='安徽省'
)
cd731107 2011-07-23
  • 打赏
  • 举报
回复
select * from 公司详细信息表
where areaid
in (
select a.areaid from 地区表 a, 地区表 b
where a.parentid=b.areaid and b.area='安徽省'
union all
select a.areaid from 地区表 a, 地区表 b,地区表 c
where a.parentid=b.areaid and b.parentid=c.areaid and c.area='安徽省'
)
yangfeng1819 2011-07-23
  • 打赏
  • 举报
回复
那有没其他什么简单的办法。 修改数据库也行。 只要能实现这个功能就行、 数据库是2000的。
AcHerat 元老 2011-07-23
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 yangfeng1819 的回复:]

..这么个东西这么复杂。我是数据库小白。 只会简单的增删改查操作。
[/Quote]

你这本身就需要一个递归来实现,2000用函数来处理,获取需要的节点表值函数!
yangfeng1819 2011-07-23
  • 打赏
  • 举报
回复
..这么个东西这么复杂。我是数据库小白。 只会简单的增删改查操作。
cd731107 2011-07-23
  • 打赏
  • 举报
回复
前面用的是sql 2005的语法,sql 2000中没有的,上面换了一个使用自定义函数的方法
cd731107 2011-07-23
  • 打赏
  • 举报
回复
--查询指定节点及其所有子节点的函数
create function f_cid(@ID int) returns @t_level table(areaid int , level int)
as
begin
declare @level int
set @level = 1
insert into @t_level select @id , @level
while @@ROWCOUNT > 0
begin
set @level = @level + 1
insert into @t_level select a.areaid , @level
from 地区表 a , @t_Level b
where a.parentid = b.areaid and b.level = @level - 1
end
return
end

go

select * from 公司详细信息表 where areaid in (select areaid from dbo.f_cid(1))
yangfeng1819 2011-07-23
  • 打赏
  • 举报
回复
sql server 2000 cte这是什么东西? 存储过程?
cd731107 2011-07-23
  • 打赏
  • 举报
回复
with cte as 
(
select * from 地区表 where areaid=1
union all
select a.* from 地区表 a,cte b where a.parentid=b.areaid
)

select * from 公司详细信息表 where areaid in (select areaid from cte)
另外,你用的是sql什么版本
yangfeng1819 2011-07-23
  • 打赏
  • 举报
回复
什么意思 完全看不懂?能不能不用存储过程 这个太复杂了。 有没简单点的?
cd731107 2011-07-23
  • 打赏
  • 举报
回复
with cte as 
(
select * from 地区表 where areaid=1
union all
select a.* from 地区表 a,cte b where a.parentid=b.areaid
)

select * from cte
cd731107 2011-07-23
  • 打赏
  • 举报
回复
/*
标题:SQL SERVER 2000中查询指定节点及其所有子节点的函数(表格形式显示)
作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-05-12
地点:广东深圳
*/

create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
insert into tb values('001' , null , '广东省')
insert into tb values('002' , '001' , '广州市')
insert into tb values('003' , '001' , '深圳市')
insert into tb values('004' , '002' , '天河区')
insert into tb values('005' , '003' , '罗湖区')
insert into tb values('006' , '003' , '福田区')
insert into tb values('007' , '003' , '宝安区')
insert into tb values('008' , '007' , '西乡镇')
insert into tb values('009' , '007' , '龙华镇')
insert into tb values('010' , '007' , '松岗镇')
go

--查询指定节点及其所有子节点的函数
create function f_cid(@ID varchar(3)) returns @t_level table(id varchar(3) , level int)
as
begin
declare @level int
set @level = 1
insert into @t_level select @id , @level
while @@ROWCOUNT > 0
begin
set @level = @level + 1
insert into @t_level select a.id , @level
from tb a , @t_Level b
where a.pid = b.id and b.level = @level - 1
end
return
end
go

--调用函数查询001(广东省)及其所有子节点
select a.* from tb a , f_cid('001') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
001 NULL 广东省
002 001 广州市
003 001 深圳市
004 002 天河区
005 003 罗湖区
006 003 福田区
007 003 宝安区
008 007 西乡镇
009 007 龙华镇
010 007 松岗镇

(所影响的行数为 10 行)
*/
yangfeng1819 2011-07-23
  • 打赏
  • 举报
回复
公司详细信息表:CompanyId areaid indusId CompanyNmae 等。。。。
地区表:areaid area parentid
1 所有地区 0
2 安徽省 1
3 合肥市 2
4 长丰县 3
5 肥东县 3
6 肥西县 3
这该怎么查出所有安徽省的公司。?
AcHerat 元老 2011-07-23
  • 打赏
  • 举报
回复
表结构,数据及测试结果,公司详细表,应该还有地区表吧!
xuam 2011-07-23
  • 打赏
  • 举报
回复
给出数据及效果来
AcHerat 元老 2011-07-23
  • 打赏
  • 举报
回复
帖子上下边有 管理菜单·结贴·发帖·回复,点击结贴,分配好分数,点确定就OK。
yangfeng1819 2011-07-23
  • 打赏
  • 举报
回复
谢谢两位 辛苦给我解答。 我都给分了。
yangfeng1819 2011-07-23
  • 打赏
  • 举报
回复
SQL code--查询指定节点及其所有子节点的函数
create function f_cid(@ID int) returns @t_level table(areaid int , level int)
as
begin
declare @level int
set @level = 1
insert into @t_level select @id , @level
while @@ROWCOUNT > 0
begin
set @level = @level + 1
insert into @t_level select a.areaid , @level
from 地区表 a , @t_Level b
where a.parentid = b.areaid and b.level = @level - 1
end
return
end

go

select * from 公司详细信息表 where areaid in (select areaid from dbo.f_cid(1))

这方法我用了 很好很强大 是我要的效果。 不过怎么给分啊 。
yangfeng1819 2011-07-23
  • 打赏
  • 举报
回复
我试试

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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