mysql count sum code example
Example 1: mysql count vs sum
+
| id | vote |
+
| 1 | 1 | COUNT(vote) = 5
| 2 | -1 | SUM(vote) = 1 = (-2 + 3 = 1)
| 3 | 1 |
| 4 | -1 |
| 5 | 1 |
+
SELECT COUNT(case when USER_NAME = 'JoeBlow' then 'no matter what' else NULL end)
from your_table
Example 2: using sum function in mysql for two diffrenet conditions
SELECT
SUM(CASE
WHEN status = 'Shipped' THEN quantityOrdered
END) qty_shipped,
SUM(CASE
WHEN status = 'Resolved' THEN quantityOrdered
END) qty_resolved,
SUM(CASE
WHEN status = 'Cancelled' THEN quantityOrdered
END) qty_cancelled,
SUM(CASE
WHEN status = 'On Hold' THEN quantityOrdered
END) qty_on_hold,
SUM(CASE
WHEN status = 'Disputed' THEN quantityOrdered
END) qty_on_disputed,
SUM(CASE
WHEN status = 'In Process' THEN quantityOrdered
END) qty_in_process
FROM
orderdetails
INNER JOIN
orders USING (orderNumber);
Example 3: sum mysql column
SELECT sum( mark ) FROM `student`