Subscribe For Free Updates!

We'll not spam mate! We promise.

Sunday, November 2, 2014

MySQL ASC And DESC

We have seen SQL SELECT command to fetch data from MySQL table. When you select rows, the MySQL server is free to return them in any order, unless you instruct it otherwise by saying how to sort the result. But you sort a result set by adding an ORDER BY clause that names the column or columns you want to sort by.

Syntax:

Here is generic SQL syntax of SELECT command along with ORDER BY clause to sort data from MySQL table:
SELECT field1, field2,...fieldN table_name1, table_name2...
ORDER BY field1, [field2...] [ASC [DESC]]
  • You can sort returned result on any field provided that filed is being listed out.
  • You can sort result on more than one field.
  • You can use keyword ASC or DESC to get result in ascending or descending order. By default, it's ascending order.
  • You can use WHERE...LIKE clause in usual way to put condition.

Using ORDER BY clause at Command Prompt:

This will use SQL SELECT command with ORDER BY clause to fetch data from MySQL table tutorials_tbl.

Example:

Try out the following example, which returns result in ascending order.
root@host# mysql -u root -p password;
Enter password:*******
mysql> use TUTORIALS;
Database changed
mysql> SELECT * from javatutorials_tbl ORDER BY javatutorial_author ASC
+----------------+--------------------+---------------------+-----------------+
|javatutorial_id | javatutorial_title | javatutorial_author | submission_date |
+----------------+--------------------+---------------------+-----------------+
|           2    | Learn MySQL        | Karthik             | 2007-05-24      |
|           1    | Learn PHP          | Gemini              | 2007-05-24      |
|           3    | JAVA Tutorial      | Darshan             | 2007-05-06      |
+----------------+--------------------+---------------------+-----------------+
3 rows in set (0.42 sec)

mysql>
Verify all the author names are listed out in ascending order.

Using ORDER BY clause inside PHP Script:

You can use similar syntax of ORDER BY clause into PHP function mysql_query(). This function is used to execute SQL command and later another PHP function mysql_fetch_array() can be used to fetch all the selected data.

Example:

Try out the following example, which returns result in descending order of javatutorial author.
<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT javatutorial_id, javatutorial_title, 
               javatutorial_author, submission_date
        FROM javatutorials_tbl
        ORDER BY  javatutorial_author DESC';

mysql_select_db('JAVATUTORIALS');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
    echo "Tutorial ID :{$row['javatutorial_id']}  <br> ".
         "Title: {$row['javatutorial_title']} <br> ".
         "Author: {$row['javatutorial_author']} <br> ".
         "Submission Date : {$row['submission_date']} <br> ".
         "--------------------------------<br>";
} 
echo "Fetched data successfully\n";
mysql_close($conn);
?>

Socializer Widget
SOCIALIZE IT →
FOLLOW US →
SHARE IT →

0 comments:

Post a Comment