Php heade is a function that call for put the action on another page …..
<html>
<?php
/* This will give an error. Note the output
* above, which is before the header() call */
header('Location: http://www.example.com/');
?> <?php
$to = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns.
| SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name |
We have the following “Orders” table:
| O_Id | OrderDate | OrderPrice | Customer |
|---|---|---|---|
| 1 | 2008/11/12 | 1000 | Hansen |
| 2 | 2008/10/23 | 1600 | Nilsen |
| 3 | 2008/09/02 | 700 | Hansen |
| 4 | 2008/09/03 | 300 | Hansen |
| 5 | 2008/08/30 | 2000 | Jensen |
| 6 | 2008/10/04 | 100 | Nilsen |
Now we want to find the total sum (total order) of each customer.
We will have to use the GROUP BY statement to group the customers.
We use the following SQL statement:
| SELECT Customer,SUM(OrderPrice) FROM Orders 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.
Explanation of why the above SELECT statement cannot be used: The SELECT statement above has two columns specified (Customer and SUM(OrderPrice). The “SUM(OrderPrice)” returns a single value (that is the total sum of the “OrderPrice” column), while “Customer” returns 6 values (one value for each row in the “Orders” table). This will therefore not give us the correct result. However, you have seen that the GROUP BY statement solves this problem.
We can also use the GROUP BY statement on more than one column, like this:
| SELECT Customer,OrderDate,SUM(OrderPrice) FROM Orders GROUP BY Customer,OrderDate |
The MIN() function returns the smallest value of the selected column.
| SELECT MIN(column_name) FROM table_name |
We have the following “Orders” table:
| O_Id | OrderDate | OrderPrice | Customer |
|---|---|---|---|
| 1 | 2008/11/12 | 1000 | Hansen |
| 2 | 2008/10/23 | 1600 | Nilsen |
| 3 | 2008/09/02 | 700 | Hansen |
| 4 | 2008/09/03 | 300 | Hansen |
| 5 | 2008/08/30 | 2000 | Jensen |
| 6 | 2008/10/04 | 100 | Nilsen |
Now we want to find the smallest value of the “OrderPrice” column.
We use the following SQL statement:
| SELECT MIN(OrderPrice) AS SmallestOrderPrice FROM Orders |
The result-set will look like this:
| SmallestOrderPrice |
|---|
| 100 |
The MAX() function returns the largest value of the selected column.
| SELECT MAX(column_name) FROM table_name |
We have the following “Orders” table:
| O_Id | OrderDate | OrderPrice | Customer |
|---|---|---|---|
| 1 | 2008/11/12 | 1000 | Hansen |
| 2 | 2008/10/23 | 1600 | Nilsen |
| 3 | 2008/09/02 | 700 | Hansen |
| 4 | 2008/09/03 | 300 | Hansen |
| 5 | 2008/08/30 | 2000 | Jensen |
| 6 | 2008/10/04 | 100 | Nilsen |
Now we want to find the largest value of the “OrderPrice” column.
We use the following SQL statement:
| SELECT MAX(OrderPrice) AS LargestOrderPrice FROM Orders |
The result-set will look like this:
| LargestOrderPrice |
|---|
| 2000 |
The LAST() function returns the last value of the selected column.
| SELECT LAST(column_name) FROM table_name |
We have the following “Orders” table:
| O_Id | OrderDate | OrderPrice | Customer |
|---|---|---|---|
| 1 | 2008/11/12 | 1000 | Hansen |
| 2 | 2008/10/23 | 1600 | Nilsen |
| 3 | 2008/09/02 | 700 | Hansen |
| 4 | 2008/09/03 | 300 | Hansen |
| 5 | 2008/08/30 | 2000 | Jensen |
| 6 | 2008/10/04 | 100 | Nilsen |
Now we want to find the last value of the “OrderPrice” column.
We use the following SQL statement:
| SELECT LAST(OrderPrice) AS LastOrderPrice FROM Orders |
Tip: Workaround if LAST() function is not supported:
| SELECT OrderPrice FROM Orders ORDER BY O_Id DESC LIMIT 1 |
The result-set will look like this:
| LastOrderPrice |
|---|
| 100 |
The FIRST() function returns the first value of the selected column.
| SELECT FIRST(column_name) FROM table_name |
We have the following “Orders” table:
| O_Id | OrderDate | OrderPrice | Customer |
|---|---|---|---|
| 1 | 2008/11/12 | 1000 | Hansen |
| 2 | 2008/10/23 | 1600 | Nilsen |
| 3 | 2008/09/02 | 700 | Hansen |
| 4 | 2008/09/03 | 300 | Hansen |
| 5 | 2008/08/30 | 2000 | Jensen |
| 6 | 2008/10/04 | 100 | Nilsen |
Now we want to find the first value of the “OrderPrice” column.
We use the following SQL statement:
| SELECT FIRST(OrderPrice) AS FirstOrderPrice FROM Orders |
Tip: Workaround if FIRST() function is not supported:
| SELECT OrderPrice FROM Orders ORDER BY O_Id LIMIT 1 |
The result-set will look like this:
| FirstOrderPrice |
|---|
| 1000 |
The FORMAT() function is used to format how a field is to be displayed.
| SELECT FORMAT(column_name,format) FROM table_name |
| Parameter | Description |
|---|---|
| column_name | Required. The field to be formatted. |
| format | Required. Specifies the format. |
We have the following “Products” table:
| Prod_Id | ProductName | Unit | UnitPrice |
|---|---|---|---|
| 1 | Jarlsberg | 1000 g | 10.45 |
| 2 | Mascarpone | 1000 g | 32.56 |
| 3 | Gorgonzola | 1000 g | 15.67 |
Now we want to display the products and prices per today’s date (with today’s date displayed in the following format “YYYY-MM-DD”).
We use the following SELECT statement:
| SELECT ProductName, UnitPrice, FORMAT(Now(),’YYYY-MM-DD’) as PerDate FROM Products |
The result-set will look like this:
| ProductName | UnitPrice | PerDate |
|---|---|---|
| Jarlsberg | 10.45 | 2008-10-07 |
| Mascarpone | 32.56 | 2008-10-07 |
| Gorgonzola | 15.67 | 2008-10-07 |
The CREATE TABLE statement is used to create a table in a database.
| CREATE TABLE table_name ( column_name1 data_type, column_name2 data_type, column_name3 data_type, …. ) |
The data type specifies what type of data the column can hold. For a complete reference of all the data types available in MS Access, MySQL, and SQL Server, go to our complete Data Types reference.
Now we want to create a table called “Persons” that contains five columns: P_Id, LastName, FirstName, Address, and City.
We use the following CREATE TABLE statement:
| CREATE TABLE Persons ( P_Id int, LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255) ) |
The P_Id column is of type int and will hold a number. The LastName, FirstName, Address, and City columns are of type varchar with a maximum length of 255 characters.
The empty “Persons” table will now look like this:
| P_Id | LastName | FirstName | Address | City |
|---|---|---|---|---|
The empty table can be filled with data with the INSERT INTO statement.