MySQL Group BY tutorial, is grouping Oh.
When you have mastered the basics of MySQL, it takes time to take the next step and take on the aggregate function. Before we talk about them, let's review the definition, overall, as it relates to MySQL:
To constitute or equal to a whole; ~ American Traditional Dictionary
This type of wording, we can assume that MySQL's summary function of something will be very high up, or in other words, the opposite detail.
The most common type of aggregate function lets you find such a minimum, maximum, and average set of data for "grouping". The trick to understanding aggregate functions is often to understand what data is being grouped and analyzed.
MySQL Group-data
Before we can start throwing the functions around these fantasies, let's build a proper table with enough data to make sense of it. Below is the SQL for our "products" table. You can run this SQL statement in your MySQL administrator's software or use MySQL to execute queries (such as creating a table and then each record).
You can download the file Www.111cn.net from our website. If you are new to MySQL, you need to know how to create a MySQL table and insert a MySQL row.
Here is the MySQL table of the products.
ID |
name |
type |
| Price
123451 |
Park ' s great Hits |
Music |
19.99 |
123452 |
Silly Puddy |
Toy |
3.99 |
123453 |
Playstation |
Toy |
89.95 |
123454 |
Men ' s T-Shirt |
Clothing |
32.50 |
123455 |
Blouse |
Clothing |
34.97 |
123456 |
Electronica 2002 |
Music |
3.99 |
123457 |
Country Tunes |
Music |
21.55 |
123458 |
Watermelon |
Food |
8.73 |
Group-To create your first "group"
Just imagine that our store ads are in the newspaper and we want to have a "bargain basement" section that lists the lowest prices for each product type. In this case, we will "group" the product type and find the lowest price of each group.
Our query needs to be restored in two columns: Product type and lowest price. In addition, we want to use the Type column as our group. Select statement we are going to use a different look because it contains an aggregate function, Min, the group's declaration, otherwise it is not any different, a normal SELECT statement.
<?php
Make a MySQL Connection
$query = "Select Type, MIN (price) from the products GROUP by type";
$result = mysql_query ($query) or Die (Mysql_error ());
Print out Result
while ($row = Mysql_fetch_array ($result)) {
echo $row [' type ']. " - $". $row ['MIN (price)'];
echo "<br/>";
}
?>
Output.
Clothing-$32.50
Food-$8.73
Music-$3.99
Toy-$3.99
MySQL GROUP BY-examines a set of data that the
Group is good at retrieving information. If you have only one product per type, then the group will not be all beneficial. The
Group only shines when you have many similar things. For example, if you have some of the same type of products and want to find some statistics such as lowest, highest, or other high-level information, you can use the group's.
Some technical rules of the group by:
Column, your group must also be in your SELECT statement.
Remember the information you want in the columns, not the overall functionality you are applying for. In our example above we want the type columns and aggregate functions of the information to apply to the price bar.
Lessons for the next few years will provide a walkthrough using the other popular MySQL aggregation functions with expert group presentations.