Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

MariaDB ORDER BY sort clause


May 16, 2021 MariaDB


Table of contents


As mentioned in the previous discussion, the ORDER BY clause sorts the results of statements. I t specifies the order in which the data is operated and includes options for sorting in ascending (ASC) or descending order (DESC). W hen order specifications are omitted, the default order is ascending.

The ORDER BY clause appears in a variety of statements, such as DELETE and UPDATE. T hey always appear at the end of the statement, not in sub-queries or before set functions, because they operate on the final result table. Y ou cannot use integers to identify columns.

See the general syntax of the OLD BY clause given below -

SELECT field, field2,... [or column] FROM table_name, table_name2,...
ORDER BY field, field2,... ASC[or DESC]

Use the ORDER BY clause at the command prompt or in the PHP script.

Command prompt

At the command prompt, simply use the standard command -

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             |
+-------------+----------------+----------------------+

THE ORDER BY clause is used in PHP scripts

In statements that use the ORDER BY clause, use the mysql_query() function again -

<?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
";
   mysql_close($conn);
?>

After a successful data retrieval, you will see the following output -

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