Home > database >  Help: a column a is the sum of all the other data in the data
Help: a column a is the sum of all the other data in the data

Time:09-20

There is an example, the table name: table

Id name data
0 hebei 50468
1 the jiangxi 71429
2 shandong 69591
3 the ningxia 48825
4 in gansu province 96813
5 guizhou 55565
6 hubei 72192
7 hunan 90473
8 in guangxi 99914
9 the sichuan 91375
10 the null

Now want to MySQL through a piece of code to implement the national data=https://bbs.csdn.net/topics/other 0-9 and,,,,

CodePudding user response:

data=https://bbs.csdn.net/topics/other 0-9 is what mean? Suggested that describe the building Lord want results are presented and the corresponding test data

CodePudding user response:

The name for the data=https://bbs.csdn.net/topics/+ + in jiangxi province of hebei,,,,,,, + data in sichuan

Or id for 10 data=https://bbs.csdn.net/topics/id for the sum of the data of 0 to 9

CodePudding user response:

Mysql statement is used to implement

CodePudding user response:

The test data
 -If not object_id (N 'Tempdb for.. # T ') is null 
Drop table # T
Go
The Create table # T (int [id], [name] nvarchar (22), [the data] int)
Insert # T
The select 0, N 'hebei', 50468 union all
Select 1, N jiangxi, 71429 union all
Select 2, N 'shandong, 69591 union all
Select 3, N ningxia, 48825 union all
Select 4, N ', gansu province, 96813 union all
Select 5, N guizhou, 55565 union all
Select 6, N hubei, 72192 union all
Select 7, N hunan, 90473 union all
The select 8, N guangxi, 99914 union all
The select 9, N sichuan, 91375 union all
The select 10, N 'national', null
Go
- the end of the test data
UPDATE # T SET data=https://bbs.csdn.net/topics/(SELECT SUM (data) FROM the WHERE id # T BETWEEN 0 AND 9) WHERE id=10

SELECT * FROM # T



CodePudding user response:

The UPDATE table A, (SELECT SUM (data) sumdata FROM table WHERE id<10) SET B A.data=https://bbs.csdn.net/topics/B.sumdata WHERE id=10;

CodePudding user response:


@ February 16 a great god, and I use this statement error:
 update temp set data=https://bbs.csdn.net/topics/(select sum (data) from temp where the data is not null) where id=10; 

Error: 1093 - You can 't specify target table' temp 'for update in the FROM clause,,

Could you give some tips?
  • Related