本文介紹了將一行字段設(shè)置為 2 個其他字段的乘積的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我有一個這樣的SQL表結(jié)構(gòu)
Id A B C D1 1 5 6 252 2 10 5 253 3 7 4 254 1 6 5 265 2 10 5 266 3 8 3 26
我想寫一個腳本,它將更新所有B &A=3 的行中的 C 列 與 A = 1 和 A = 2 相乘的值(對于相同的 D 列)
所以結(jié)果應(yīng)該是
<前>身份證 A B C D1 1 5 6 252 2 10 5 253 3 50 30 254 1 6 5 265 2 10 5 266 3 60 25 26如何在 SQL 中編寫這樣的代碼?
解決方案
一種可能的方法是將表與自身連接兩次:
更新T3放T3.B = T1.B * T2.B,T3.C = T1.C * T2.C來自 [表] T3在 T1.A = 1 和 T1.D = T3.D 上加入 [表] T1在 T2.A = 2 和 T2.D = T3.D 上加入 [表] T2在哪里T3.A = 3
I have a such a structure of SQL table
Id A B C D
1 1 5 6 25
2 2 10 5 25
3 3 7 4 25
4 1 6 5 26
5 2 10 5 26
6 3 8 3 26
I want to write a script, which will update all the B & C columns in the rows with A=3 with the value of multiplication of the A = 1 and A = 2 (for the same value of D column)
So the result should be
Id A B C D 1 1 5 6 25 2 2 10 5 25 3 3 50 30 25 4 1 6 5 26 5 2 10 5 26 6 3 60 25 26
How can I write such a code in SQL?
解決方案
One possible way is joining table to itself twice:
update T3
set
T3.B = T1.B * T2.B,
T3.C = T1.C * T2.C
from [Table] T3
join [Table] T1 on T1.A = 1 and T1.D = T3.D
join [Table] T2 on T2.A = 2 and T2.D = T3.D
where
T3.A = 3
這篇關(guān)于將一行字段設(shè)置為 2 個其他字段的乘積的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!