有一個例子,表名:table
id name data
0 河北 50468
1 江西 71429
2 山東 69591
3 寧夏 48825
4 甘肅 96813
5 貴州 55565
6 湖北 72192
7 湖南 90473
8 廣西 99914
9 四川 91375
10 全國 null
現在想通過MySQL一段代碼實作全國的data=https://bbs.csdn.net/topics/其他0-9的和。。。。
uj5u.com熱心網友回復:
data=https://bbs.csdn.net/topics/其他0-9 是啥意思?建議樓主描述一下并對應測驗資料給出想要的結果uj5u.com熱心網友回復:
name為全國的data = 河北+江西+。。。。。+四川 的data或者說id為10的data = id為0到9的data 之和
uj5u.com熱心網友回復:
用mysql陳述句實作uj5u.com熱心網友回復:
--測驗資料
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([id] int,[name] nvarchar(22),[data] int)
Insert #T
select 0,N'河北',50468 union all
select 1,N'江西',71429 union all
select 2,N'山東',69591 union all
select 3,N'寧夏',48825 union all
select 4,N'甘肅',96813 union all
select 5,N'貴州',55565 union all
select 6,N'湖北',72192 union all
select 7,N'湖南',90473 union all
select 8,N'廣西',99914 union all
select 9,N'四川',91375 union all
select 10,N'全國',null
Go
--測驗資料結束
UPDATE #T SET data=https://bbs.csdn.net/topics/(SELECT SUM(data) FROM #T WHERE id BETWEEN 0 AND 9) WHERE id=10
SELECT * FROM #T
uj5u.com熱心網友回復:
UPDATE table A,(SELECT SUM(data) sumdata FROM table WHERE id<10) B SET A.data=https://bbs.csdn.net/topics/B.sumdata WHERE id =10;uj5u.com熱心網友回復:
@二月十六 大神,我用這個陳述句出錯的:
update temp set data=https://bbs.csdn.net/topics/(select sum(data) from temp where data is not null) where id=10;
錯誤提示:1093 - You can't specify target table 'temp' for update in FROM clause。。
可否給點提示?
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/54226.html
標籤:MySQL
上一篇:5. 索引與演算法—B+樹的操作、輔助索引與聚集索引、Cardinality、聯合索引、覆寫索引、MRR/ICP、哈希演算法、全文索引
