来解决给分不很难.

yunfengh 2004-01-12 12:22:05
现有 TABLE1,结构如下:
字段名 A B
code 编号
id 序号
table2
字段名 Aa Bb
id code
1 2

现要用一个SQL语句(SELECT)来查询出来

编号(code) 序号 (id)
1 2


请高手出招吧1小弟重重有奖!


...全文
41 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
yunfengh 2004-01-14
  • 打赏
  • 举报
回复
谢谢大家.
zjcxc 元老 2004-01-13
  • 打赏
  • 举报
回复
--下面是测试

--测试数据
create table t1(fieldname sysname,cnname sysname)
insert into t1
select 'spcd','产地'
union all select 'spcode','商品编号'
union all select 'spid','商品内码'
union all select 'spname','商品名称'
union all select 'sporder','总店商品编号'
union all select 'spspgg','规格'
union all select 'spsycx','适用车型'
union all select 'spxw','西文'

create table t2(spcd varchar(10),spid int,spname int)
insert into t2
select 'fdsdf',4234,235325
union all select 'efwre',24234,234234
union all select '23423',23423,23443
go

--查询处理
declare @s varchar(8000)
set @s=''
select @s=@s+',['+cnname+']=['+name+']'
from syscolumns a join t1 b on a.name=b.fieldname
where object_id('t2')=a.id
set @s=substring(@s,2,8000)
exec('select '+@s+' from t2')
go

--删除测试结果
drop table t1,t2

/*--测试结果
产地 商品内码 商品名称
---------- ----------- -----------
fdsdf 4234 235325
efwre 24234 234234
23423 23423 23443
--*/
zjcxc 元老 2004-01-13
  • 打赏
  • 举报
回复
declare @s varchar(8000)
set @s=''
select @s=@s+',['+cnname+']=['+name+']'
from syscolumns a join t1 b on a.name=b.fieldname
where object_id('t2')=a.id
set @s=substring(@s,2,8000)
exec('select '+@s+' from t2')
go
wanghuixue 2004-01-12
  • 打赏
  • 举报
回复
up
txlicenhe 2004-01-12
  • 打赏
  • 举报
回复
测试:
create table t1(A varchar(10),B varchar(10))
insert t1 values ('code','编号')
insert t1 values ('id','序号')
create table t2(Aa varchar(10),Bb varchar(10))
insert t2 values('id','code')
insert t2 values('1','2')

select IsNull(t.B+'('+t2.Bb+')',t2.Bb),IsNull(s.B+'('+t2.Aa+')',t2.Aa) from t2
left join t1 s on t2.Aa = s.A
left join t1 t on t2.Bb = t.A

/*结果
---------------------- ----------------------
编号(code) 序号(id)
2 1

(所影响的行数为 2 行)

*/
txlicenhe 2004-01-12
  • 打赏
  • 举报
回复
测试:
create table t1(A varchar(10),B varchar(10))
insert t1 values ('code','编号')
insert t1 values ('id','序号')
create table t2(Aa varchar(10),Bb varchar(10))
insert t2 values('id','code')
insert t2 values('1','2')

select IsNull(s.B+'('+t2.Aa+')',t2.Aa),IsNull(t.B+'('+t2.Bb+')',t2.Bb) from t2
left join t1 s on t2.Aa = s.A
left join t1 t on t2.Bb = t.A
/*结果
---------------------- ----------------------
序号(id) 编号(code)
1 2
*/
Jianli2004 2004-01-12
  • 打赏
  • 举报
回复
产地 商品内码 商品名称
---------- ----------- -----------
fdsdf 4234 235325
efwre 24234 234234
23423 23423 23443
Jianli2004 2004-01-12
  • 打赏
  • 举报
回复
declare @a varchar(8000)
set @a='select spcd as '+(select cnname from t1 where fieldname='spcd')
+',spid '+(select cnname from t1 where fieldname='spid')
+',spname '+(select cnname from t1 where fieldname='spname')
+' from t2'
--print @a
exec(@a)
yunfengh 2004-01-12
  • 打赏
  • 举报
回复
欢迎大家用QQ和我交流

QQ: 5788857
yunfengh 2004-01-12
  • 打赏
  • 举报
回复
字段说明表 t1 为
fieldname cnname
spcd 产地
spcode 商品编号
spid 商品内码
spname 商品名称
sporder 总店商品编号
spspgg 规格
spsycx 适用车型
spxw 西文

t2为
spcd spid spname
fdsdf 4234 235325
efwre 24234 234234
23423 23423 23443




用一个语句查询结果为:

产地 商品编号 商品内码 商品名称 这是列名
sdsdf 23234 23423 243534 这是查询的值
234 3453 3534 34534
-狙击手- 2004-01-12
  • 打赏
  • 举报
回复
看不懂
zjcxc 元老 2004-01-12
  • 打赏
  • 举报
回复
再举例说明,数据多一点,看不明白
sunrisehy2003 2004-01-12
  • 打赏
  • 举报
回复
樓主的意思有的不明了
leeboyan 2004-01-12
  • 打赏
  • 举报
回复
楼主是行列转换吧,搜一下好多的!
yunfengh 2004-01-12
  • 打赏
  • 举报
回复
不对.
我要是的这样的结果!
列名为 编号(code) 序号(id)
记录1: 2 1
记录2: 3 2

这样的效果行不?
yunfengh 2004-01-12
  • 打赏
  • 举报
回复
不好意思去参加了微软的培训,没有时间来看.但我用了一下不行.
如果记录多了就不了.
请给出具体的解决方案
分不够可以再加.
zjcxc 元老 2004-01-12
  • 打赏
  • 举报
回复
楼主是马可的意思吧?
kimurakenshin 2004-01-12
  • 打赏
  • 举报
回复
up up up

34,589

社区成员

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

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