Wu Yuxiong - natural born MySQL study notes: MySQL sorting

To read the data using SQL SELECT statement from the MySQL table. 
If we need to sort of read data, we can use MySQL ORDER BY clause according to which you want to set a field which way to sort, and then returned in the search results. 
Syntax 
The following is a SQL SELECT statement with an ORDER BY clause to return query data will then sort the data: 
the SELECT field1, Field2, ... fieldN the FROM table_name1, table_name2 ... 
ORDER BY field1 [ASC [DESC] [Default ASC]], [field2 ...] [ASC [DESC ] [ default ASC]] 
can use any sort of field as a condition, to return query results sorted. 
Setting a plurality of fields can be sorted. 
ASC or DESC keyword can be used to set the search result in ascending or descending order. By default, it is ascending. 
You can add WHERE ... LIKE clause to set conditions.
At the command prompt using the ORDER BY clause in 
the ORDER BY clause in the SQL SELECT statement to read data in MySQL data tables runoob_tbl: 
MySQL > use RUNOOB; 
Database changed 
MySQL > the SELECT * from runoob_tbl ORDER BY ASC submission_date ;
PHP script used in the ORDER BY clause 
can use a function of the mysqli_query PHP () and bring same SQL SELECT command ORDER BY clause to obtain data. 
This function is used to execute SQL command, and then () outputs the data in all queries by PHP function mysqli_fetch_array. 
< ? PHP 
$ dbhost = ' localhost: 3306 ' ; // MySQL server host address 
$ dbuser = ' root ' ; // MySQL username 
$ dbpass = ' 123456 ' ; // MySQL username and password 
$ conn = mysqli_connect ($ dbhost , dbuser $, $ dbpass);
 IF ! ($ Conn) 
{ 
    Die ( ' connection failed: '. Mysqli_error ($ Conn)); 
}
 // set the encoding, Chinese distortion preventing 
the mysqli_query ($ Conn, " SET UTF8 names " ); 
 
$ SQL = ' the SELECT runoob_id, runoob_title, 
        runoob_author, submission_date 
        the FROM runoob_tbl 
        the ORDER BY submission_date the ASC ' ; 
 
mysqli_select_db ($ Conn, ' RUNOOB ' ); 
$ retval = the mysqli_query (Conn $, $ SQL);
 IF ($ retval!) 
{ 
    Die ( ' not read: ' . mysqli_error ($ Conn)); 
} 
the while ($ Row = mysqli_fetch_array($retval, MYSQL_ASSOC))
{
    echo "<tr><td> {$row['runoob_id']}</td> ".
         "<td>{$row['runoob_title']} </td> ".
         "<td>{$row['runoob_author']} </td> ".
         "<td>{$row['submission_date']} </td> ".
         "</tr>";
}
echo '</table>';
mysqli_close($conn);
?>

 

Guess you like

Origin www.cnblogs.com/tszr/p/12113792.html