Reorganized title: Calculating the total value of other columns using MySQL's Group By and Sum
P粉908138620
P粉908138620 2023-08-27 19:13:59
0
2
555
<p>I have two columns like this: </p> <table class="s-table"> <thead> <tr> <th>Word</th> <th>Amount</th> </tr> </thead> <tbody> <tr> <td>dog</td> <td>1</td> </tr> <tr> <td>dog</td> <td>5</td> </tr> <tr> <td>Elephant</td> <td>2</td> </tr> </tbody> </table> <p>I want to sum the amounts and get the result</p> <table class="s-table"> <thead> <tr> <th>Word</th> <th>Amount</th> </tr> </thead> <tbody> <tr> <td>dog</td> <td>6</td> </tr> <tr> <td>Elephant</td> <td>2</td> </tr> </tbody> </table> <p>What I have tried (and failed) so far is: </p> <pre class="brush:php;toolbar:false;">SELECT word, SUM(amount) FROM `Data` GROUP BY 'word'</pre> <p><br /></p>
P粉908138620
P粉908138620

reply all(2)
P粉165522886

It should be accented symbol instead of single quote :

SELECT word, SUM( amount )
FROM Data
GROUP BY `word`;

Output:

word     SUM(amount)
dog           6
Elephant      2

P粉377412096

Remove the single quotes around WORD. It causes column names to be converted to strings.

SELECT word, SUM(amount) 
FROM Data 
Group By word
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template