把两张表的数据连接在一起

LotusUnix 2008-05-12 09:49:43
表 a00_Module :
a00code a00name
1001 系统管理
1002 基础信息
2001 订单管理
2002 采购管理
2003 配货管理
2004 报表管理

表a01_funinfo:
a00code a01code a01name
1001 1 初始设置
1001 2 权限管理
1002 3 商品类别
1002 4 扩展规格
1002 5 商品品牌
1002 6 商品信息
1002 7 商户信息
1002 8 客户信息
1002 9 数据词典

想把这两张表连接在一起,相等的条件是a00code
并且连接后想用两张表统一的名称和编号

1001 系统管理
1 初始设置
2 权限管理
1002 基础信息
2 权限管理
3 商品类别
4 扩展规格
5 商品品牌
6 商品信息
7 商户信息
8 客户信息
9 数据词典
并且按着两张表的a00Sort,a01Sort排序!
...全文
329 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
tim_spac 2008-05-13
  • 打赏
  • 举报
回复
从所需结果看,其实没必要做join, union后适当的输出排序即可。
cson_cson 2008-05-13
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 swot2008 的回复:]
第四行的
2 权限管理
是怎么得到的
[/Quote]
应该是LZ写错了吧
tim_spac 2008-05-12
  • 打赏
  • 举报
回复
set nocount on
go
create table #a00_Module(a00code int,a00name varchar(10))
insert #a00_Module select 1001, '系统管理'
insert #a00_Module select 1002 , '基础信息'
insert #a00_Module select 2001 , '订单管理'
insert #a00_Module select 2002 , '采购管理'
insert #a00_Module select 2003 , '配货管理'
insert #a00_Module select 2004 , '报表管理'

create table #a01_funinfo(a00code int,a01code int,a01name varchar(10))
insert #a01_funinfo select 1001, 1 ,'初始设置'
insert #a01_funinfo select 1001 , 2 , '权限管理'
insert #a01_funinfo select 1002 , 3 , '商品类别'
insert #a01_funinfo select 1002 , 4 , '扩展规格'
insert #a01_funinfo select 1002 , 5 , '商品品牌'
insert #a01_funinfo select 1002 , 6 , '商品信息'
insert #a01_funinfo select 1002 , 7 , '商户信息'
insert #a01_funinfo select 1002 , 8 , '客户信息'
insert #a01_funinfo select 1002 ,9, '数据词典'
go
select code = case a01code when 0 then a00code else a01code end
,a00name
from (
select a00code, a01code=0, a00name from #a00_Module
union
select a00code, a01code, a01name from #a01_funinfo
) as a
order by a00code, a01code

drop table #a01_funinfo, #a00_Module
-- code a00name
-- 1001 系统管理
-- 1 初始设置
-- 2 权限管理
-- 1002 基础信息
-- 3 商品类别
-- 4 扩展规格
-- 5 商品品牌
-- 6 商品信息
-- 7 商户信息
-- 8 客户信息
-- 9 数据词典
-- 2001 订单管理
-- 2002 采购管理
-- 2003 配货管理
-- 2004 报表管理
--
-狙击手- 2008-05-12
  • 打赏
  • 举报
回复
楼上的加个条件就行
union
select 0 px,a00code,a00code,a00name from a00_module b
where exists( select 1 from a01_funinfo where a00code = b.a00code)
cson_cson 2008-05-12
  • 打赏
  • 举报
回复
create table #a00_Module(a00code int,a00name varchar(10))
insert #a00_Module select 1001, '系统管理'
insert #a00_Module select 1002 , '基础信息'
insert #a00_Module select 2001 , '订单管理'
insert #a00_Module select 2002 , '采购管理'
insert #a00_Module select 2003 , '配货管理'
insert #a00_Module select 2004 , '报表管理'

create table #a01_funinfo(a00code int,a01code int,a01name varchar(10))
insert #a01_funinfo select 1001, 1 ,'初始设置'
insert #a01_funinfo select 1001 , 2 , '权限管理'
insert #a01_funinfo select 1002 , 3 , '商品类别'
insert #a01_funinfo select 1002 , 4 , '扩展规格'
insert #a01_funinfo select 1002 , 5 , '商品品牌'
insert #a01_funinfo select 1002 , 6 , '商品信息'
insert #a01_funinfo select 1002 , 7 , '商户信息'
insert #a01_funinfo select 1002 , 8 , '客户信息'
insert #a01_funinfo select 1002 ,9, '数据词典'
go
select a01code,a01name from(
select 1 px, a00code,a01code,a01name from #a01_funinfo
union
select 0 px,a00code,a00code,a00name from #a00_module)a
order by a00code,px,a01code
/*
a01code a01name
----------- ----------
1001 系统管理
1 初始设置
2 权限管理
1002 基础信息
3 商品类别
4 扩展规格
5 商品品牌
6 商品信息
7 商户信息
8 客户信息
9 数据词典
2001 订单管理
2002 采购管理
2003 配货管理
2004 报表管理
*/
go
drop table #a00_Module,#a01_funinfo
LotusUnix 2008-05-12
  • 打赏
  • 举报
回复

tim_spac 没有加条件
tim_spac 不是横向连接,是纵向连接!!
继续期待中!
不过,还是谢谢大家的热情!!
青锋-SS 2008-05-12
  • 打赏
  • 举报
回复
select a00code,a00name,a01code,a01name
from a00_module a0,a01_funinfo a1
where a0.a00code=a1.a00code
order by a00code,a01code
tim_spac 2008-05-12
  • 打赏
  • 举报
回复
select code = case a01code when 0 then a00code else a01code end
,a00name
from (
select a00code, a01code=0, a00name from a00_Module
union
select a00code, a01code, a00name from a00_Module
) as a
order by a00code, a01code
-狙击手- 2008-05-12
  • 打赏
  • 举报
回复
union all
swot2008 2008-05-12
  • 打赏
  • 举报
回复
第四行的
2 权限管理
是怎么得到的
huoxudong125 2008-05-12
  • 打赏
  • 举报
回复
sql server中子查询,oracle中就更好说了,有关键字

34,588

社区成员

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

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