We use the following SQL statement:
SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer
GROUP BY Customer
The result-set will look like this:
Customer | SUM(OrderPrice) |
---|---|
Hansen | 2000 |
Nilsen | 1700 |
Jensen | 2000 |
Nice! Isn't it? :)
Let's see what happens if we omit the GROUP BY statement:
SELECT Customer,SUM(OrderPrice) FROM Orders
The result-set will look like this:
Customer | SUM(OrderPrice) |
---|---|
Hansen | 5700 |
Nilsen | 5700 |
Hansen | 5700 |
Hansen | 5700 |
Jensen | 5700 |
Nilsen | 5700 |
The result-set above is not what we wanted.
2
http://www.tizag.com/mysqlTutorial/mysqlgroupby.php
$query = "SELECT type, MIN(price) FROM products GROUP BY type";
Display:
Clothing - $32.50
Food - $8.73
Music - $3.99
Toy - $3.99
Food - $8.73
Music - $3.99
Toy - $3.99
No comments:
Post a Comment