热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

sql拼接两条查询出来的结果

sql拼接两条查询出来的结果前提:不用存储过程!表结构:idnametype1aaa12bbb2selectnamefrombiaowh
sql拼接两条查询出来的结果

前提 :不用存储过程!


表结构:

id name  type
1   aaa   1
2   bbb   2

select name from biao where type=1

select name from biao where type=2

这样会有两个name结果 我想得出aaabbb怎么一条语句搞定


请高手

50 个解决方案

#1


declare @name nvarchar(20)

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


引用 3 楼 feixianxxx 的回复:
SQL codeif 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
*/

要是1000个呢???

#5


引用 4 楼 fredrickhu 的回复:
引用 3 楼 feixianxxx 的回复:
SQL codeif 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 
*/ 


要是1000个呢???

那就用你的

#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


引用 3 楼 feixianxxx 的回复:
SQL codeif 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
*/

不通

#8


能有一条语句解决的办法吗?

#9


两位真是辛苦

#10


引用 7 楼 xming4321 的回复:
引用 3 楼 feixianxxx 的回复:
SQL codeif 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 
*/ 


不通

不通什么意思?
第二种
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


引用 11 楼 xming4321 的回复:
select (select name from a where type=1)+(select name from a where type=2 ) 
这个在sql下报错 
另外的 等哈


你是不是不止2条记录的?
你把你表完整贴出来看看
我的这个方法只能做2条记录的虾

#13


引用 11 楼 xming4321 的回复:
select (select name from a where type=1)+(select name from a where type=2 ) 
这个在sql下报错 
另外的 等哈

...用我2楼的 别信麦递这小子的

#14


引用 13 楼 fredrickhu 的回复:
引用 11 楼 xming4321 的回复:
select (select name from a where type=1)+(select name from a where type=2 ) 
这个在sql下报错 
另外的 等哈 


...用我2楼的 别信麦递这小子的

。。。冤枉。。。我让她试你的方法啦。。。

#15


另外的也不成 而且我要是一条语句啊!

然后我的意思 是把查出的

两行数据中,相同的列 拼成一列成一条记录

#16


引用 12 楼 feixianxxx 的回复:
引用 11 楼 xming4321 的回复:
select (select name from a where type=1)+(select name from a where type=2 ) 
这个在sql下报错 
另外的 等哈 



你是不是不止2条记录的? 
你把你表完整贴出来看看 
我的这个方法只能做2条记录的虾


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

    return @str
end
go


--调用函数
select distinct convert (varchar(10), MakeDay,120) as  MakeDay, BranchCD, Message= dbo.f_str(BranchCD) 
from ttt 
/*---------------------
2009-06-01 8 apple-orange-
2009-06-01 9 apple-pear-
2009-06-02 10 apple-lizhi-
------------------*/

#18


就是字符串联合起来啊?

#19


对就是这个意思

#20


引用 19 楼 xming4321 的回复:
对就是这个意思

那你看下上面的例子
我再给你几个例子 这是字符组合的一般方法
各种字符串合并处理示例\




--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 @t TABLE(col1 varchar(10),col2 varchar(100))

--定义游标并进行合并处理
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


/*==============================================*/


#21


引用 18 楼 feixianxxx 的回复:
就是字符串联合起来啊?

但是只是联合两个 呵呵 没你上面的那么复杂

#22


引用 20 楼 feixianxxx 的回复:
引用 19 楼 xming4321 的回复:
对就是这个意思 


那你看下上面的例子 
我再给你几个例子 这是字符组合的一般方法 

SQL code各种字符串合并处理示例\




--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

--合并处理
--定…

有没有一句可以搞定的  我们这里不用存储过程

#23


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


引用 25 楼 feixianxxx 的回复:
只能就一句么? 呵呵


我们这里 要求高没有办法

#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


引用 30 楼 feixianxxx 的回复:
LZ 人呢?

还在 呵呵

#33


等你帮我找个好方法了

#34


引用 33 楼 xming4321 的回复:
等你帮我找个好方法了


前面的方法 不算一条是吧?
你的一条什么概念。。

#35


一条 能够执行的 不需要定义变量 

关键 

#36


引用 35 楼 xming4321 的回复:
一条 能够执行的 不需要定义变量 

关键 

我先出去下 如果我回来 你还没解决 我再帮你 不好意思 有急事

#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


引用 40 楼 guguda2008 的回复:
SQL code
--借用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

这个不太对

#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
*/

#44


.

#45


引用 37 楼 xming4321 的回复:
我是用 程序执行语句  当然 一次连接 就能要处结果


批处理 语句打包一下不可以么?我想了半天 一句还是实现不了 等高手 阿

#46


引用 41 楼 feixianxxx 的回复:
引用 40 楼 guguda2008 的回复:
SQL code 
--借用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 t…

他的虽然没打倒我的要求可是 基本实现了

#47


引用 45 楼 feixianxxx 的回复:
引用 37 楼 xming4321 的回复:
我是用 程序执行语句  当然 一次连接 就能要处结果 



批处理 语句打包一下不可以么?我想了半天 一句还是实现不了 等高手 阿

40楼的基本可用

#48


引用 47 楼 xming4321 的回复:
引用 45 楼 feixianxxx 的回复:
引用 37 楼 xming4321 的回复: 
我是用 程序执行语句  当然 一次连接 就能要处结果 


批处理 语句打包一下不可以么?我想了半天 一句还是实现不了 等高手 阿 


40楼的基本可用

hehe 能用就好 呵呵

#49


发现对你的贴 我挺激情的

#50


 concat 使用方法查API   方便到你想哭

推荐阅读
author-avatar
熊字饼爱优乐美
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有