首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

sql 语句如何写! 有点麻烦

2012-05-03 
sql 语句怎么写! 有点麻烦select * from t_source_question_kpqidkp_id112111221124410974109623现在需要

sql 语句怎么写! 有点麻烦
select * from t_source_question_kp

qidkp_id
1121
1122
1124
41097
41096
23

现在需要将qid=1的kp_id字段值查到
select kp_id from t_source_question_kp where qid=1
kp_id
121
122
124

但是需要将kp_id字段值放在一起显示 如这样:
select kp_id from t_source_question_kp where qid=1
kp_id
121 122 124

这样sql语句怎么写 页面代码已经固定 现在只能改sql语句了

[解决办法]

SQL code
if object_id('[t_source_question_kp]') is not null drop table [t_source_question_kp]gocreate table [t_source_question_kp] (qid int,kp_id int)insert into [t_source_question_kp]select 1,121 union allselect 1,122 union allselect 1,124 union allselect 4,1097 union allselect 4,1096 union allselect 2,3select * from [t_source_question_kp]alter FUNCTION dbo.f_str(@id int) RETURNS varchar(8000) AS BEGIN     DECLARE @r varchar(8000)     SET @r = ''     SELECT @r = @r +' '+ convert(varchar,kp_id) FROM t_source_question_kp WHERE qid=@id     RETURN STUFF(@r, 1, 1, '') END GO select qid,dbo.f_str(qid) as  kp_id from t_source_question_kp group by qid/*1    121 122 1242    34    1097 1096*/ 

热点排行