The ORDER BY condition, as referenced in past conversations, sorts the aftereffects of an assertion. It determines the request for the information worked on, and remembers the choice to sort for climbing (ASC) or plunging (DESC) request. On exclusion of request detail, the default request is climbing.
Request BY provisions show up in a wide assortment of proclamations, for example, DELETE and UPDATE. They generally show up toward the finish of an assertion, not in a subquery or before a set capacity, since they work on the last coming about table. You additionally can't utilize a whole number to recognize a section.
Survey the overall grammar of the ORDER BY condition given beneath −
SELECT field, field2,... [or column] FROM table_name, table_name2,...
ORDER BY field, field2,... ASC[or DESC]
Utilize an ORDER BY condition either at the order brief or inside a PHP content.
The Command Prompt
At the order brief, essentially utilize a standard order −
root@ host# mysql -u root -p password;
Enter password:*******
mysql> use PRODUCTS;
Database changed
mysql> SELECT * from products_tbl ORDER BY product_manufacturer ASC
+-------------+----------------+----------------------+
| ID_number | Nomenclature | product_manufacturer |
+-------------+----------------+----------------------+
| 56789 | SuperBlast 400 | LMN Corp |
+-------------+----------------+----------------------+
| 67891 | Zoomzoom 5000 | QFT Corp |
+-------------+----------------+----------------------+
| 12347 | Orbitron 1000 | XYZ Corp |
+-------------+----------------+----------------------+
PHP Script Using Order By Clause
Use the mysql_query() work, indeed, in articulations utilizing the ORDER BY provision −
<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT product_id, product_name, product_manufacturer, ship_date
FROM products_tbl ORDER BY product_manufacturer DESC';
mysql_select_db('PRODUCTS');
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
echo "Product ID :{$row['product_id']} <br> ".
"Name: {$row['product_name']} <br> ".
"Manufacturer: {$row['product_manufacturer']} <br> ".
"Ship Date : {$row['ship_date']} <br> ".
"--------------------------------<br>";
}
echo "Fetched data successfully\n";
mysql_close($conn);
?>
On fruitful information recovery, you will see the accompanying yield −
Product ID: 12347
Nomenclature: Orbitron 1000
Manufacturer: XYZ Corp
Ship Date: 01/01/17
----------------------------------------------
Product ID: 67891
Nomenclature: Zoomzoom 5000
Manufacturer: QFT Corp
Ship Date: 01/01/17
----------------------------------------------
Product ID: 56789
Nomenclature: SuperBlast 400
Manufacturer: LMN Corp
Ship Date: 01/04/17
----------------------------------------------
mysql> Fetched data successfully