简体   繁体   中英

MySQL Adding columns together

Quick MySQL problem.

I'm wanting to add multiple columns (column 2) together based one column (column 1) being checked to ensure they hold matching values

Example

Column 1

Team 1     
Team 2      
Team 3       
Team 1    
Team 3   
Team 2  

Column 2

 1
 2
 3 
 1
 2
 3

Column 3 (Calculated based on the Values in Column 1 matching)

 2 (Team 1)
 4 (Team 2)
 6 (Team 3)

Of course I will begin by doing the following select statement:

SELECT Column 1, Column 2 FROM table WHERE ???

After the where is when I don't know what comes next to add the columns together based on the first column holding the same value.

Looks to me like you just need a simple sum and group by. But I think in your example, Team 2 should have a total of 5, not 4.

SELECT column1,sum(column2) FROM table
group by column1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM