select @name=isnull(@name,'')+name from biao where type in(1,2) select name=@name
#2
DECLARE @name VARCHAR(8000)
SELECT @name=ISNULL(@name,'')+name FROM (SELECT DISTINCT NAME FROM A where type in(1,2))AS T
SELECT @name
#3
if OBJECT_ID('a') is not null drop table a create table a(id int,name varchar(5),type int) insert a select 1,'aaa',1 union all select 2,'bbb',2 go select (select name from a where type=1)+(select name from a where type=2 ) /* aaabbb */
#4
要是1000个呢???
#5
那就用你的
#6
if OBJECT_ID('a') is not null drop table a create table a(id int,name varchar(5),type int) insert a select 1,'aaa',1 union all select 2,'bbb',2 go --select (select name from a where type=1)+(select name from a where type=2 )
DECLARE @name VARCHAR(8000)
SELECT @name=ISNULL(@name,'')+name FROM (SELECT DISTINCT NAME FROM A where type in(1,2))AS T
SELECT @name /* aaabbb */
#7
不通
#8
能有一条语句解决的办法吗?
#9
两位真是辛苦
#10
不通什么意思?
第二种
DECLARE @name VARCHAR(8000)
SELECT @name=ISNULL(@name,'')+name FROM (SELECT DISTINCT NAME FROM A where type in(1,2))AS T
SELECT @name
也不行么?
#11
select (select name from a where type=1)+(select name from a where type=2 )
这个在sql下报错
另外的 等哈
#12
你是不是不止2条记录的?
你把你表完整贴出来看看
我的这个方法只能做2条记录的虾
#13
...用我2楼的 别信麦递这小子的
#14
。。。冤枉。。。我让她试你的方法啦。。。
#15
另外的也不成 而且我要是一条语句啊!
然后我的意思 是把查出的
两行数据中,相同的列 拼成一列成一条记录
#16
type=1 type=2 这样就只查出两条记录
#17
你是不是这个意思?
create table ttt ( MakeDay datetime ,BranchCD int ,Message varchar(1000) ) insert into ttt values('2009/06/01',8,'apple-') insert into ttt values('2009/06/01',8,'orange-') insert into ttt values('2009/06/01',9,'apple-') insert into ttt values('2009/06/01',9,'pear-') insert into ttt values('2009/06/02',10,'apple-') insert into ttt values('2009/06/02',10,'lizhi-') go
create function dbo.f_str(@BranchCD int) returns varchar(100) as begin declare @str varchar(1000) set @str = '' select @str = @str + Message from ttt where BranchCD = @BranchCD
--3.3.1 使用游标法进行字符串合并处理的示例。 --处理的数据 CREATE TABLE tb(col1 varchar(10),col2 int) INSERT tb SELECT 'a',1 UNION ALL SELECT 'a',2 UNION ALL SELECT 'b',1 UNION ALL SELECT 'b',2 UNION ALL SELECT 'b',3
--定义游标并进行合并处理 DECLARE tb CURSOR LOCAL FOR SELECT col1,col2 FROM tb ORDER BY col1,col2 DECLARE @col1_old varchar(10),@col1 varchar(10),@col2 int,@s varchar(100) OPEN tb FETCH tb INTO @col1,@col2 SELECT @col1_old=@col1,@s='' WHILE @@FETCH_STATUS=0 BEGIN IF @col1=@col1_old SELECT @s=@s+','+CAST(@col2 as varchar) ELSE BEGIN INSERT @t VALUES(@col1_old,STUFF(@s,1,1,'')) SELECT @s=','+CAST(@col2 as varchar),@col1_old=@col1 END FETCH tb INTO @col1,@col2 END INSERT @t VALUES(@col1_old,STUFF(@s,1,1,'')) CLOSE tb DEALLOCATE tb --显示结果并删除测试数据 SELECT * FROM @t DROP TABLE tb /*--结果 col1 col2 ---------- ----------- a 1,2 b 1,2,3 --*/ GO
--3.3.2 使用用户定义函数,配合SELECT处理完成字符串合并处理的示例 --处理的数据 CREATE TABLE tb(col1 varchar(10),col2 int) INSERT tb SELECT 'a',1 UNION ALL SELECT 'a',2 UNION ALL SELECT 'b',1 UNION ALL SELECT 'b',2 UNION ALL SELECT 'b',3 GO
--合并处理函数 CREATE FUNCTION dbo.f_str(@col1 varchar(10)) RETURNS varchar(100) AS BEGIN DECLARE @re varchar(100) SET @re='' SELECT @re=@re+','+CAST(col2 as varchar) FROM tb WHERE col1=@col1 RETURN(STUFF(@re,1,1,'')) END GO
--调用函数 SELECT col1,col2=dbo.f_str(col1) FROM tb GROUP BY col1 --删除测试 DROP TABLE tb DROP FUNCTION f_str /*--结果 col1 col2 ---------- ----------- a 1,2 b 1,2,3 --*/ GO
--3.3.3 使用临时表实现字符串合并处理的示例 --处理的数据 CREATE TABLE tb(col1 varchar(10),col2 int) INSERT tb SELECT 'a',1 UNION ALL SELECT 'a',2 UNION ALL SELECT 'b',1 UNION ALL SELECT 'b',2 UNION ALL SELECT 'b',3
--合并处理 SELECT col1,col2=CAST(col2 as varchar(100)) INTO #t FROM tb ORDER BY col1,col2 DECLARE @col1 varchar(10),@col2 varchar(100) UPDATE #t SET @col2=CASE WHEN @col1=col1 THEN @col2+','+col2 ELSE col2 END, @col1=col1, col2=@col2 SELECT * FROM #t /*--更新处理后的临时表 col1 col2 ---------- ------------- a 1 a 1,2 b 1 b 1,2 b 1,2,3 --*/ --得到最终结果 SELECT col1,col2=MAX(col2) FROM #t GROUP BY col1 /*--结果 col1 col2 ---------- ----------- a 1,2 b 1,2,3 --*/ --删除测试 DROP TABLE tb,#t GO
select (select name from a where type=1)+(select name from a where type=2 ) 这个的意思对 可是运行不了
#24
你可以把表的数据再完整点/
然后把最后结果的样子贴出来看看么?
#25
只能就一句么? 呵呵
#26
就是 这样
id type key
1 1 a,b,c
2 2 d,e,f
3 0 aaaaa
我现在要得到a,b,c,d,e,f
#27
我们这里 要求高没有办法
#28
if OBJECT_ID('tt') is not null drop table tt go create table tt(id int,[type] int,[key] varchar(100)) insert tt select 1,1,'a,b,c' union all select 2,2,'d,e,f' union all select 3,1,'a,b,c' go declare @s varchar(100) set @s='' select @s=@s+','+[key] from tt where id=[type]
select STUFF(@s,1,1,'') ------------- a,b,c,d,e,f
#29
丫。。。好像不止一句了。。
#30
LZ 人呢?
#31
还是不可以啊!
#32
还在 呵呵
#33
等你帮我找个好方法了
#34
前面的方法 不算一条是吧?
你的一条什么概念。。
#35
一条 能够执行的 不需要定义变量
关键
#36
我先出去下 如果我回来 你还没解决 我再帮你 不好意思 有急事
#37
我是用 程序执行语句 当然 一次连接 就能要处结果
#38
综合前边人的意见,
DECLARE @name VARCHAR(8000)
select @name=ISNULL(@name,'')+name from T1 where Type in (select distinct type from T1)
select @name
#39
去吃饭 O(∩_∩)O~
#40
--借用T-MAC兄的数据 if OBJECT_ID('tt') is not null drop table tt go create table tt(id int,[type] int,[key] varchar(100)) insert tt select 1,1,'a,b,c' union all select 2,2,'d,e,f' union all select 3,1,'a,b,c' go --有ID就用ID,没有就用ROWNUM select t1.id,t1.[key],t1.[key]+isnull((select [key] from tt t2 where t2.id=t1.id+1),'') from tt t1 where t1.id%2=1 order by t1.id
#41
这个不太对
#42
--測試數據
declare @table table
(
id int
,name varchar(3)
,[type] int
);
insert into @table
values(1,'aaa',1)
,(2,'bbb',2)
,(3,'ccc',3);
--查詢
select name as [text()]
from @table
where [type] in (1,2)
for xml path('')
#43
LS的似乎有点靠谱
if OBJECT_ID('tt') is not null drop table tt go create table tt(id int,[type] int,[key] varchar(100)) insert tt select 1,1,'a,b,c' union all select 3,3,'d,e,f' union all select 3,3,'d,e,f' union all select 4,4,'k,p,q' union all select 5,5,'y,r,d' go
select [KEY] as [text()] from tt where id in(select id from tt where id=[type]) for xml path('') /* a,b,cd,e,fd,e,fk,p,qy,r,d */