sql语句 如何将一个表中的多条数据显示在一行呢???
idNameAgeGender1张三18男2李四20女3王五22女表如上如何显示为:1张三18男2李四女3王五22女...
id Name Age Gender
1 张三 18 男
2 李四 20 女
3 王五 22 女
表如上 如何显示为:
1 张三 18 男 2 李四 女 3 王五 22 女 展开
1 张三 18 男
2 李四 20 女
3 王五 22 女
表如上 如何显示为:
1 张三 18 男 2 李四 女 3 王五 22 女 展开
展开全部
如果知道需要显示的确切的数据数量,那么直接在sql的select语句中这样写:
select 1 as id1, (select name from table where id=1)as name1,(select age from table where id=1)as age1,(select gender from table where id=1)as gender1,
2 as id2, (select name from table where id=2)as name2,(select age from table where id=2)as age2,(select gender from table where id=2)as gender2,
3 as id3, (select name from table where id=3)as name3,(select age from table where id=3)as age3,(select gender from table where id=3)as gender3
from table
如果数据量不确定,则可以用游标做一个对原表的遍历,这样写(假设记录数最多100条):
create table #result (id1 int ,name1 varchar(10),age1 int , gender1 char(2)......id100 int ,name100 varchar(10),age100 int , gender100 char(2))
declare @id int, @name varchar(10), @age int , @gender char(2),@i int, @c varchar(5),
@cId varchar(5), @cAge varchar(3)
set @i=0
set @c='0'
declare mycur cursor for select id,name,age,gender from table order by id
open mycur
fetch next from mycur into @id , @name , @age , @gender
while (@@fetch_status=0)
begin
set @i=@i+1
set @c=convert(varchar(5),@i)
exec('insert into #result (id'+@c+',name'+@c+',age'+@c+',gender'+@c+') values ('+@cId+','''+@name+''','+@cAge+',‘’‘+@gender+’‘’)‘)
fetch next from mycur into @id , @name , @age , @gender
end
close mycur
deallocate mycur
select * from #result
select 1 as id1, (select name from table where id=1)as name1,(select age from table where id=1)as age1,(select gender from table where id=1)as gender1,
2 as id2, (select name from table where id=2)as name2,(select age from table where id=2)as age2,(select gender from table where id=2)as gender2,
3 as id3, (select name from table where id=3)as name3,(select age from table where id=3)as age3,(select gender from table where id=3)as gender3
from table
如果数据量不确定,则可以用游标做一个对原表的遍历,这样写(假设记录数最多100条):
create table #result (id1 int ,name1 varchar(10),age1 int , gender1 char(2)......id100 int ,name100 varchar(10),age100 int , gender100 char(2))
declare @id int, @name varchar(10), @age int , @gender char(2),@i int, @c varchar(5),
@cId varchar(5), @cAge varchar(3)
set @i=0
set @c='0'
declare mycur cursor for select id,name,age,gender from table order by id
open mycur
fetch next from mycur into @id , @name , @age , @gender
while (@@fetch_status=0)
begin
set @i=@i+1
set @c=convert(varchar(5),@i)
exec('insert into #result (id'+@c+',name'+@c+',age'+@c+',gender'+@c+') values ('+@cId+','''+@name+''','+@cAge+',‘’‘+@gender+’‘’)‘)
fetch next from mycur into @id , @name , @age , @gender
end
close mycur
deallocate mycur
select * from #result
展开全部
用游标可以实现。
假设你这个表名字为student
--定义变量@sql用来存要执行的sql语句
declare @sql varchar(5000) set @sql='select 'student_info: ', '
--定义 @column来存游标取得每一行数据
declare @column varchar(20)
--定义游标
declare cursor1 cursor for
select id from student
--打开游标
open cursor1
--获取第一行的id放到@column里面
fetch next from cursor1 into @column
--开始循环
while @@FETCH_STATUS=0
begin
--取每一行id,Name, Age, Gender
set @sql=@sql+'
,'''+@column+'''
,(select Name from student as s where s.id='''+@column+''')
,(select Age from student as s where s.id='''+@column+''')
,(select Gender from student as s where s.id='''+@column+''')
'
fetch next from cursor1 into @column
end
--关闭游标
close cursor1
--释放游标
deallocate cursor1
--执行@sql
exec (@sql)
最后结果应该为
student_info: 1 张三 18 男 2 李四 女 3 王五 22 女
PS:游标是很邪恶的,非常占资源,如果你能确定id是加一增长的,并且能确定id的最大值,建议你麻烦一点用循环或者临时表写。
假设你这个表名字为student
--定义变量@sql用来存要执行的sql语句
declare @sql varchar(5000) set @sql='select 'student_info: ', '
--定义 @column来存游标取得每一行数据
declare @column varchar(20)
--定义游标
declare cursor1 cursor for
select id from student
--打开游标
open cursor1
--获取第一行的id放到@column里面
fetch next from cursor1 into @column
--开始循环
while @@FETCH_STATUS=0
begin
--取每一行id,Name, Age, Gender
set @sql=@sql+'
,'''+@column+'''
,(select Name from student as s where s.id='''+@column+''')
,(select Age from student as s where s.id='''+@column+''')
,(select Gender from student as s where s.id='''+@column+''')
'
fetch next from cursor1 into @column
end
--关闭游标
close cursor1
--释放游标
deallocate cursor1
--执行@sql
exec (@sql)
最后结果应该为
student_info: 1 张三 18 男 2 李四 女 3 王五 22 女
PS:游标是很邪恶的,非常占资源,如果你能确定id是加一增长的,并且能确定id的最大值,建议你麻烦一点用循环或者临时表写。
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不需要那么复杂:
create table student (id int, Name varchar(255), Age int, Gender varchar(255));
insert into student values (1, '张三', 18, '男');
insert into student values (2, '李四', 20, '女');
insert into student values (3, '王五', 22, '女');
--建表插入数据
declare @str varchar(max)
set @str=''
select @str=@str+CONVERT(varchar(255),id)+' '+Name+' '+CONVERT(varchar(255),Age)+' '+Gender +' ' from student
select @str
create table student (id int, Name varchar(255), Age int, Gender varchar(255));
insert into student values (1, '张三', 18, '男');
insert into student values (2, '李四', 20, '女');
insert into student values (3, '王五', 22, '女');
--建表插入数据
declare @str varchar(max)
set @str=''
select @str=@str+CONVERT(varchar(255),id)+' '+Name+' '+CONVERT(varchar(255),Age)+' '+Gender +' ' from student
select @str
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
declare @id varchar(50)
declare @Name varchar(50)
declare @Age varchar(50)
declare @Gender varchar(50)
select @c=''
declare tb_cursor cursor for
select id,Name,Age,Gender from table
open tb_cursor
fetch tb_cursor into @id,@Name,@Age,@Gender
while( @@fetch_status=0 )
begin
set @c=@c+' '+@id+' '+@Name+' '+@Age+' '+@Gender
fetch tb_cursor into @id,@Name,@Age,@Gender
end
select @c
close tb_cursor
DEALLOCATE tb_cursor
用游标吧,我默认你的都是字符串类型。其它类型要记得转化。希望能帮助你。
declare @Name varchar(50)
declare @Age varchar(50)
declare @Gender varchar(50)
select @c=''
declare tb_cursor cursor for
select id,Name,Age,Gender from table
open tb_cursor
fetch tb_cursor into @id,@Name,@Age,@Gender
while( @@fetch_status=0 )
begin
set @c=@c+' '+@id+' '+@Name+' '+@Age+' '+@Gender
fetch tb_cursor into @id,@Name,@Age,@Gender
end
select @c
close tb_cursor
DEALLOCATE tb_cursor
用游标吧,我默认你的都是字符串类型。其它类型要记得转化。希望能帮助你。
追问
在c#开发中如何利用游标呢?没使过,求指导。。。
追答
不好意思。C#没深入了解过。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
select t1.*, t2.*, t3.*
from (select * from table where id=1) t1
cross join (select * from table where id=2) t2
corss join (select * from table where id=3) t3
from (select * from table where id=1) t1
cross join (select * from table where id=2) t2
corss join (select * from table where id=3) t3
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询