求sql语句:除了指定的列不选择,其它字段都选择的sql语句

fangss 2008-02-20 01:27:20
有两张表table1和table2。主键都是id,但是其它字段不确定。
table1{id,fld1, fld2...}
table2{id,fld3, fld4...}
//注意,
1. id是确定的,fld1,fld2等字段是不确定的,也就是我不指定这张表里面其它字段的名称。
2. 两张表里主键记录相同。

现在要选择两张表里面所有的字段,如果使用:
select * from table1, table2 where table1.id=table2.id
这样的结果基本上是对的,但是结果里面有两个id。

id fld1 fld2 id fld3 fld4
-------------------------
1 a b 1 c d
2 aa bb 2 cc cc

如何把第二个id去掉呢?记得其它字段的名称(数量)不知道啊
...全文
599 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wzjpsq 2008-02-20
  • 打赏
  • 举报
回复
declare @col varchar(1000)
select @col=isnull(@col+',','')+name from syscolumns where id=object_id('table2')
and name!='id'
declare @sql varchar(8000)
set @sql='select a.*,'+@col+' from table1 a,table2 b where a.id=b.id'
exec(@sql)
zefuzhang2008 2008-02-20
  • 打赏
  • 举报
回复
exec('select table1.*'+select (select ','+cast(column_name as varchar) as [text()]
from information_schema.columns
where table_name='table2' and column_name <>'id'
for xml path(''))+' from table1,table2 where table1.id=table2.id '
)
zefuzhang2008 2008-02-20
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
select @sql='select table1.*'+select (select ','+cast(column_name as varchar) as [text()]
from information_schema.columns
where table_name='table2' and column_name <>'id'
for xml path(''))+' from table1,table2 where table1.id=table2.id '
exec (@sql)
wzy_love_sly 2008-02-20
  • 打赏
  • 举报
回复
不行
wzy_love_sly 2008-02-20
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
select @sql=isnull(@sql+',','')+name
from syscolumns where id=object_id('bm')
and name <>'bmid'
exec('select '+@sql+' from bm')


bmname username hpmc gz
销售一部 小张 货品A 1000
销售二部 小王 货品B 3000
销售三部 小乐 货品C 2000
销售四部 小马 货品D 1000
销售一部 小张 货品B 3000
销售二部 小王 货品D 1000
销售三部 小乐 货品C 2000
销售四部 小马 货品D 1500
fangss 2008-02-20
  • 打赏
  • 举报
回复
一句sql不可以实现吗
中国风 2008-02-20
  • 打赏
  • 举报
回复
declare @s nvarchar(4000)
select
@s=isnull(@s+',','')+'t1.'+quotename(Name)
from
syscolumns
where
ID=object_id('table1')
order by colid

select
@s=isnull(@s+',','')+'t2.'+quotename(Name)
from
syscolumns
where
ID=object_id('table2') and Name<>'ID'
order by colid

exec('select '+@s+' from table1 t1,table2 t2 where t1.ID=t2.ID')
pt1314917 2008-02-20
  • 打赏
  • 举报
回复

declare @col varchar(1000)
select @col=isnull(@col+',','')+name from syscolumns where id=object_id('table2')
where name!='id'
declare @sql varchar(8000)
set @sql='select a.*,'+@col+' from table1 a,table2 b where a.id=b.id'
exec(@sql)

34,837

社区成员

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

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