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

sql字段和行交换

发布时间: 2022-09-05 00:30:00

sql 两行数据内容交换

为什么要交换数据,只要修改下桌号不就好了吗

两个表交换数据可以考虑如下思路
前提是两表要有相似的结构
1.将想交换的两条数据分别插入目标表
2.删除源数据
参考sql语句
insert into T1(id, name, age) select id, name, age from T2 where id=1;
insert into T2(id, name, age) select id, name, age from T1 where id=2;
delete from T1 where id=2;
delete from T2 where id=1;
只是个思路,实际操作可以比这个麻烦点。

② 如何用SQL语句交换一张表的行和列

设 A --源表, B-- 目标表

列转行: A有几个字段,B就有几条记录

行转列: A有几个记录,B就有几个字段

所以,前提条件是A表是固定的,才好操作。不然可就不好实现了。

更细致的操作就不跟这里写了。

③ sql字段转换成行

这种在SQL里貌似不行的,但是你可以用EXCLE 链接数据库使用数据透视表完成,SQL有一个函数pivot 可以进行转置 但是不能实现你的表头也转置;

④ sql语句 字段值如何互换

方法1,插入临时列,通过中间列来进行内容互换。

方法2,如果不想用中间列,那么需要2、3列类型相同,这里使用三个sql语句来进行互换,以数值型为例:
update tab set 列3=列3+列2 where (ID between 30 and70);
update tab set 列2=列3-列2 where (ID between 30 and70);
update tab set 列3=列3-列2 where (ID between 30 and70);
按照顺序执行以上三条语句,也可以实现互换,但是必须严格按照顺序依次执行,一旦执行错误,数据就无法恢复了。

⑤ 如何对sql数据库中的某一字段进行替换

update 表名 set 列1='yr' where 列1='hr'

如果换成REPLACE函数的话,具体语句如下:
update 表名 set 列1=replace(列1,'hr','yr') where 列1='hr'

以上语句的测试过了。

⑥ SQL数据库表中两行数据互换

做列表上下移动的时候遇到过,改造了下可以看看
update student t set
t.name =
(case when t.id = '1'
then (select t2.name from student t2 where t2.id ='2')
else (select t2.name from student t2 where t2.id ='1')
end) where t.id in ('1','2')

⑦ sql 数据库行列转换 如何实现呢 字段名随意取,还有字段类型都是VAC

方法一:sql嵌套,通过decode来实现;
方法二:oracle11以后的话,有个新的函数PIVOT
SQLServer的话就不是很清楚了

⑧ sql怎么实现两条记录的某一个字段互换

sql代码如下:
update 表
set 栏位1=栏位2,栏位2=栏位1

⑨ sql 语句 实现 同一列上两个值互换

用变量 先找到第一个得知
再找第二个
根据值互换

比如:我要将a表的字段name第2行数据和第5行数据进行互换(以下代码直接可运行)

--穿件临时表
create table #a
(
id int not null identity(1,1) primary key ,
[name] nvarchar(10) not null
)

insert #a select 'a'
union all select'b'
union all select'c'
union all select'd'
union all select'e'
union all select'f'
union all select'g'
union all select'h'
union all select'i'

declare @name1 nvarchar(10)
declare @name2 nvarchar(10)
set @name1 =''
set @name2 =''
--修改前
select * from #a

select @name1=[name] from #a where id=2--第二行

select @name2=[name] from #a where id=5--第5行

print @name1 +' '+@name2

update #a set [name] =@name2 where [name]=@name1

update #a set [name] =@name1 where [name]=@name2 and id<>2

--修改后
select * from #a
---完成 删除临时表
drop table #a

⑩ sql行字段内容互换

同行phone 和 mobile互换:
update customer set phone = mobile, mobile = phone where 过滤条件;