当前位置:首页 » 编程语言 » sql列合并
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

sql列合并

发布时间: 2022-01-27 10:01:39

sql Server 列合并

创建表,数据

createtable表1
(idint,
qtyint)

createtable表2
(idint,
aaaint)

insertinto表1values(1,2)
insertinto表1values(1,3)
insertinto表1values(2,4)

insertinto表2values(1,5)
insertinto表2values(2,3)
insertinto表2values(2,6)

执行:

selecta.id,a.qty,b.aaafrom
(selectid,qty,row_number()over(partitionbyidorderbyqty)rnfrom表1)ainnerjoin
(selectid,aaa,row_number()over(partitionbyidorderbyaaa)rnfrom表2)bona.id=b.idanda.rn=b.rn
union
selecta.id,a.qty,b.aaafrom
(selectid,qty,row_number()over(partitionbyidorderbyqty)rnfrom表1)aleftjoin
(selectid,aaa,row_number()over(partitionbyidorderbyaaa)rnfrom表2)bona.id=b.idanda.rn=b.rn
union
selecta.id,b.qty,a.aaafrom
(selectid,aaa,row_number()over(partitionbyidorderbyaaa)rnfrom表2)aleftjoin
(selectid,qty,row_number()over(partitionbyidorderbyqty)rnfrom表1)bona.id=b.idanda.rn=b.rn

结果:

❷ sql 怎样合并列 将一行中多列 合并成指定数量的列

先转字符型再加就可以了。
select id,cast(col1 as varchar) + '~' + cast(col2 as varchar) as col from 你的表

❸ SQL怎么列合并

1、我用Toad做演示,我先新建两张table,create table #AA(ID int,name nvarchar(10),age int)

create table #BB(ID int,name nvarchar(10),age int )。

❹ sql 两列合并成一列的问题

-- a b 类型为varchar
declare @t1 table(a varchar(10),b varchar(10))
insert into @t1
select '1','1' union
select '2','2' union
select '3','3'

select a+b c from @t1

--a b类型为int
declare @t2 table(a int,b int)
insert into @t2
select 1,1 union
select 2,2 union
select 3,3

select cast(a as varchar)+cast(b as varchar) c from @t2

--结果如下:
(3 个资料列受到影响)
c
--------------------
11
22
33

(3 个资料列受到影响)

(3 个资料列受到影响)

c
------------------------------------------------------------
11
22
33

(3 个资料列受到影响)

❺ sql中如何合并两个特定的字段

1、创建模拟的数据表

create table GoodsCate(

Mid int not null,

Code varchar(10) not null,

Name varchar(20) not null,

)

❻ 求教sql sever如何把两列合并

主要看两列是什么类型,一般就是数字或者字符,如果都是字符就直接用“+”连接即可,如果是数字的话,需要将数字转成字符再连接。
如,test表,这里id是数字类型
id name
1 张三
2 李四
现在要将两列连接

1

select cast(id as varchar)+name as str from test;

得到的结果
str
1张三
2李四
如果id列为字符类型

1
select id+name as str from test;

❼ SQL怎样合并表中两列的值为一列来显示

假设:
表中有以下列
列A
varchar(50)
列B
varchar(50)
合并显示为:
select
A+'-'+B
as
AB
from

若列的属性不一致需要转换一下:
select
cast(A
as
varchar(50))+'-'+cast(B
as
varchar(50))
as
AB
from

❽ 怎样用SQL语句合并两个表中的两个列

你给个条件好让两条合并成一条啊。如
select a.names, b.names as typ from table1 as a ,table2 as bwhere a.id=b.id

❾ SQL语句怎么实现几列数据合并成一行

你这样做的话 就这两个表是不行的。
本来就是一对多的关系 你强行的要把其变成一对一的关系那是不可能。

如果你是要在程序里实现可以这样:
001,a,10
001,b,10
001,c,10
写到一个DataTable里
然后循环读取行的第二和第三个值
在写到一个字符串里

❿ sql语句 将1列数据上下2个字段合并 (为文本)

--创建数据库
create table dbo.a(
n int
)

--将文本d:\01.txt的内容插入数据库表a
BULK
INSERT dbo.a
FROM 'd:\01.txt'
WITH (
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)

--查询得到结果
select n from(
select convert(varchar,n)+(select convert(varchar,n) from a where a.n-1=a1.n) n from a a1
union

select convert(varchar,n)+(select convert(varchar,n) from a where a.n-2=a1.n) from a a1

union

select convert(varchar,n)+(select convert(varchar,n) from a where a.n-3=a1.n) from a a1
) a2
where a2.n is not null