Detailed usage of the ORDER BY clause in PHP tutorial _mysql

Source: Internet
Author: User
Tags learn php php script php tutorial

We have seen the SQL SELECT command to get data from the MySQL table. When selecting rows, the MySQL server is free to return unless instructed to say how to sort the results otherwise it will be free to return. Specifies that a result set is required to add an ORDER BY clause to sort by the column or column name to sort by.
Syntax:

SELECT field1, Field2,... fieldn table_name1, table_name2 ...
Order by field1, [field2 ...] [ASC [DESC]]

Here is the syntax for the general SQL SELECT command, which is sorted from the data in the MySQL table:

    • You can submit any fields that will be listed based on the results returned.
    • You can sort results on more than one field.
    • You can use the keyword ASC or desc to arrange the results in ascending or descending order. It is sorted by default in ascending order.
    • You can use the where ... Add a condition in the usual way in the LIKE clause.

Use the ORDER BY clause at the command prompt:

This will use the SQL SELECT command to get the data order BY clause from the MySQL table tutorials_tbl table
Instance:

Try the following example to return the results in ascending order.

root@host# mysql-u root-p password;
Enter password:*******
mysql> use tutorials;
Database changed
mysql> SELECT * from Tutorials_tbl ORDER by Tutorial_author ASC
+-------------+------------ ----+-----------------+-----------------+
| tutorial_id | tutorial_title | tutorial_author | submission_date
| +-------------+----------------+-----------------+-----------------+
|      2 | Learn MySQL  | Abdul S     | 2007-05-24   |
|      1 | Learn PHP   | John Poul    | 2007-05-24   |
|      3 | JAVA Tutorial | Sanjay     | 2007-05-06 |
+-------------+----------------+-----------------+-----------------+
3 rows in Set (0.42 sec)

mysql>

The names of all authors are listed in ascending order.
The PHP script uses the ORDER BY clause:

You can use the ORDER BY clause of a similar syntax to the PHP function mysql_query (). After this function is used to execute the SQL command, another PHP function, mysql_fetch_array (), can be used to get all the selected data.
Example:

Try the following example tutorial authors to return the results in descending order.

<?php
$dbhost = ' localhost:3036 ';
$dbuser = ' root ';
$dbpass = ' Rootpassword ';
$conn = mysql_connect ($dbhost, $dbuser, $dbpass);
if (! $conn)
{
 die (' Could not connect: '. Mysql_error ());
}
$sql = ' SELECT tutorial_id, Tutorial_title, 
        Tutorial_author, submission_date from
    tutorials_tbl
    order by Tutorial_author DESC ';

mysql_select_db (' tutorials ');
$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 [' tutorial_id ']} <br> ".
     " Title: {$row [' Tutorial_title ']} <br> ".
     " Author: {$row [' Tutorial_author ']} <br> ".
     " Submission Date: {$row [' submission_date ']} <br> '.
     ' --------------------------------<br> ";
} 
echo "fetched data successfully\n";
Mysql_close ($conn);
? >


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.