请问一个sql语句

xiaozi145 2010-01-04 10:02:15
表1
id city citycode
1 武汉 A
2 黄石 B
3 十堰 C

表2
id area citycode areacode
1 江岸区 A 01
2 江汉区 A 02
3 黄石港区 B 01
4 西塞山区 B 02
5 茅箭区 C 01
6 张湾区 C 02

要转化成结果:
id name code
1 武汉 A
2 江岸区 01
3 江汉区 02
4 黄石 B
5 黄石港区 01
6 西塞山区 02

sql语句或者存储过程能写么??

...全文
167 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
jenny0810 2010-01-04
  • 打赏
  • 举报
回复
学习
kasin000 2010-01-04
  • 打赏
  • 举报
回复

DECLARE @a table(id INT, city varchar(20), citycode varchar(20))
insert @a select 1 ,'武汉' ,'A'
union all select 2 ,'黄石' ,'B'
union all select 3 ,'十堰' ,'C'

DECLARE @b table(id INT, area varchar(20), citycode varchar(20), areacode char(2))
insert @b select 1 ,'江岸区' ,'A' ,'01'
union all select 2 ,'江汉区' ,'A' ,'02'
union all select 3 ,'黄石港区' ,'B' ,'01'
union all select 4 ,'西塞山区' ,'B' ,'02'
union all select 5 ,'茅箭区' ,'C' ,'01'
union all select 6 ,'张湾区' ,'C' ,'02'


select
id,
city,
citycode,
'' as areacode

from @a
union

select
id,
area,
citycode,
areacode
from @b

order by citycode,areacode

/*Result*/
id city citycode areacode
----------- -------------------- -------------------- --------
1 武汉 A
1 江岸区 A 01
2 江汉区 A 02
2 黄石 B
3 黄石港区 B 01
4 西塞山区 B 02
3 十堰 C
5 茅箭区 C 01
6 张湾区 C 02

(9 row(s) affected)

ACMAIN_CHM 2010-01-04
  • 打赏
  • 举报
回复
1 楼
fly0012008 2010-01-04
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 nianran520 的回复:]
SQL code--> 测试数据:[表1]ifobject_id('[表1]')isnotnulldroptable[表1]createtable[表1]([id]int,[city]varchar(4),[citycode]varchar(1))insert[表1]select1,'武汉','A'unionallselect2,'黄石','B'unionallselect3,'十堰','C'--?-
[/Quote]
学习
antony1029 2010-01-04
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 chuifengde 的回复:]
SQL codeSET NOCOUNTONDECLARE@atable(idINT, cityvarchar(20), citycodevarchar(20))insert@aselect1 ,'武汉' ,'A'unionallselect2 ,'黄石' ,'B'unionallselect3 ,'十堰' ,'C'DECLARE@btable(idINT, areavarchar(?-
[/Quote]
简单。呵呵
xman_78tom 2010-01-04
  • 打赏
  • 举报
回复

DECLARE @a table(id INT, city nvarchar(20), citycode varchar(20))
insert @a select 1 ,N'武汉' ,'A'
union all select 2 ,N'黄石' ,'B'
union all select 3 ,N'十堰' ,'C'

DECLARE @b table(id INT, area nvarchar(20), citycode varchar(20), areacode char(2))
insert @b select 1 ,N'江岸区' ,'A' ,'01'
union all select 2 ,N'江汉区' ,'A' ,'02'
union all select 3 ,N'黄石港区' ,'B' ,'01'
union all select 4 ,N'西塞山区' ,'B' ,'02'
union all select 5 ,N'茅箭区' ,'C' ,'01'
union all select 6 ,N'张湾区' ,'C' ,'02'

select id=row_number() over (order by citycode,
(case when isnumeric(code)=1 then code else '00' end)),
city,code from (select city,citycode,citycode code from @a
union all select area,citycode,areacode from @b) t

nianran520 2010-01-04
  • 打赏
  • 举报
回复
--> 测试数据:[表1]
if object_id('[表1]') is not null drop table [表1]
create table [表1]([id] int,[city] varchar(4),[citycode] varchar(1))
insert [表1]
select 1,'武汉','A' union all
select 2,'黄石','B' union all
select 3,'十堰','C'
--> 测试数据:[表2]
if object_id('[表2]') is not null drop table [表2]
create table [表2]([id] int,[area] varchar(8),[citycode] varchar(1),[areacode] varchar(2))
insert [表2]
select 1,'江岸区','A','01' union all
select 2,'江汉区','A','02' union all
select 3,'黄石港区','B','01' union all
select 4,'西塞山区','B','02' union all
select 5,'茅箭区','C','01' union all
select 6,'张湾区','C','02'

select case id when 1 then 1 else id+(select count(1) from 表2 where citycode<t.citycode) end as id,
city as name,citycode as code from 表1 t
union all
select id+(select count(1) from 表1 where citycode<=r.citycode),area,areacode
from 表2 r
order by id
------------------------------------------
1 武汉 A
2 江岸区 01
3 江汉区 02
4 黄石 B
5 黄石港区 01
6 西塞山区 02
7 十堰 C
8 茅箭区 01
9 张湾区 02
xiaozi145 2010-01-04
  • 打赏
  • 举报
回复
如果2张表的数据不止这么多,那不是要在存储过程里把所有数据都编写进去啊?
richardlaurent 2010-01-04
  • 打赏
  • 举报
回复
楼上赐教了,好久没碰了,温故了。
richardlaurent 2010-01-04
  • 打赏
  • 举报
回复
楼上的表是
id name code
1 武汉 A
2 黄石 B
3 十堰 C
1 江岸区 01
2 江汉区 02
3 黄石港 01
4 西塞山区 02
5 茅箭区 01
6 张湾区 02
chuifengde 2010-01-04
  • 打赏
  • 举报
回复
SET NOCOUNT ON 
DECLARE @a table(id INT, city varchar(20), citycode varchar(20))
insert @a select 1 ,'武汉' ,'A'
union all select 2 ,'黄石' ,'B'
union all select 3 ,'十堰' ,'C'

DECLARE @b table(id INT, area varchar(20), citycode varchar(20), areacode char(2))
insert @b select 1 ,'江岸区' ,'A' ,'01'
union all select 2 ,'江汉区' ,'A' ,'02'
union all select 3 ,'黄石港区' ,'B' ,'01'
union all select 4 ,'西塞山区' ,'B' ,'02'
union all select 5 ,'茅箭区' ,'C' ,'01'
union all select 6 ,'张湾区' ,'C' ,'02'

SELECT id=identity(int,1,1),city,COALESCE(areacode,citycode) code INTO # FROM
(SELECT *,NULL areacode FROM @a UNION ALL SELECT * FROM @b)aa
ORDER BY citycode,areacode

SELECT * FROM #

DROP TABLE #

--result
/*id city code
----------- -------------------- --------------------
1 武汉 A
2 江岸区 01
3 江汉区 02
4 黄石 B
5 黄石港区 01
6 西塞山区 02
7 十堰 C
8 茅箭区 01
9 张湾区 02
*/
idecl 2010-01-04
  • 打赏
  • 举报
回复
select id , city as name, citycode as code from 表1
UNION ALL
select Id,area as name,areacode as code from 表2
idecl 2010-01-04
  • 打赏
  • 举报
回复
可以呀,判断最后一个字是不是"区"字来取CODE的值就可以了
gaomiqzhi 2010-01-04
  • 打赏
  • 举报
回复
学习
lovezx1028 2010-01-04
  • 打赏
  • 举报
回复
luguo

34,587

社区成员

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

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