Tuesday, October 2, 2012

mysql GROUP BY

http://www.w3schools.com/sql/sql_groupby.asp


We use the following SQL statement:
SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer
The result-set will look like this:
CustomerSUM(OrderPrice)
Hansen2000
Nilsen1700
Jensen2000
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:
CustomerSUM(OrderPrice)
Hansen5700
Nilsen5700
Hansen5700
Hansen5700
Jensen5700
Nilsen5700
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












No comments:

Post a Comment