27,579
社区成员
发帖
与我相关
我的任务
分享
如果是通过编号ID来的 可以这么写
select 地点,号码 from 表a a ,表b b
where a.id=b.id
select 市名,区名
from A join b ON A.市号=b.市号 and a.区号=b.区号
楼主表达的不太清楚。
请贴出表结构、数据及想要的结果,谢谢!
-----------------------------------------
--> 测试时间:2009-07-25
--> 我的淘宝:http://shop36766744.taobao.com/
--------------------------------------------------
if object_id('[表1]') is not null drop table [表1]
create table [表1]([id] int,[name] varchar(4))
insert [表1]
select 1,'篮球' union all
select 2,'足球' union all
select 3,'排球'
if object_id('[表2]') is not null drop table [表2]
create table [表2]([id] int,[善长球类列表] varchar(5),[name] varchar(4))
insert [表2]
select 1,'1,2,3','小飞' union all
select 2,'1,2','张三' union all
select 3,'1','李四'
create function f_str(@id varchar(40))
returns varchar(200)
as
begin
declare @str varchar(200)
set @str=','+@id+','
select @str=replace(@str,rtrim(id),name) from 表1
set @str=substring(@str,2,len(@str)-2)
return @str
end
go
select id,善长球类列表=dbo.f_str(善长球类列表) from 表2
/*
id 善长球类列表
----------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 篮球,足球,排球
2 篮球,足球
3 篮球
(所影响的行数为 3 行)
*/