① 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 過濾條件;