Latest web development tutorials

MySQL ordinamento

Sappiamo che per leggere i dati utilizzando SQL SELECT dalla tabella di MySQL.

Se abbiamo bisogno di leggere una sorta di dati, possiamo usare l'ordine di MySQL dalla clausola in cui si desidera impostare il modo in cui campo per ordinare, e poi restituito nei risultati di ricerca.

Struttura del database e dei dati utilizzati in questa sezione Download: w3big.sql .

grammatica

Quello che segue è un'istruzione SQL SELECT utilizza clausola ORDER BY per ordinare i dati di query e quindi restituire i dati:

SELECT field1, field2,...fieldN table_name1, table_name2...
ORDER BY field1, [field2...] [ASC [DESC]]
  • È possibile utilizzare qualsiasi campo, come il tipo di condizioni per restituire i risultati della query ordinati.
  • È possibile impostare più campi per ordinare.
  • È possibile utilizzare la parola chiave ASC o DESC per impostare i risultati della ricerca in ordine crescente o decrescente. Per impostazione predefinita, è in ordine crescente.
  • È possibile aggiungere DOVE ... COME clausola per impostare le condizioni.

ORDER BY nel prompt dei comandi

Di seguito utilizzerà la clausola ORDER BY nell'istruzione SQL SELECT per leggere i dati in tabelle di dati di MySQL w3big_tbl:

Esempi

Provate gli esempi che seguono, i risultati saranno disposti in ordine crescente

root@host# mysql -u root -p password;
Enter password:*******
mysql> use w3big;
Database changed
mysql> SELECT * from w3big_tbl ORDER BY w3big_author ASC;
+-----------+---------------+---------------+-----------------+
| w3big_id | w3big_title  | w3big_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.00 sec)

mysql> SELECT * from w3big_tbl ORDER BY w3big_author DESC;
+-----------+---------------+---------------+-----------------+
| w3big_id | w3big_title  | w3big_author | submission_date |
+-----------+---------------+---------------+-----------------+
|         3 | JAVA Tutorial | Sanjay        | 2007-05-06      |
|         1 | Learn PHP     | John Poul     | 2007-05-24      |
|         2 | Learn MySQL   | Abdul S       | 2007-05-24      |
+-----------+---------------+---------------+-----------------+
3 rows in set (0.00 sec)

mysql> 

W3big_tbl leggere tutti i dati nella tabella in ordine crescente in base al campo w3big_author.


Clausola ORDER BY in script PHP

Comandi che è possibile utilizzare la funzione PHP mysql_query () e mettere sullo stesso SQL SELECT clausola ORDER BY per recuperare i dati. Questa funzione viene utilizzata per eseguire comandi SQL, quindi () per dati in uscita per tutte le query attraverso funzione PHP mysql_fetch_array.

Esempi

Provate gli esempi che seguono, i rendimenti di query di dati dopo aver premuto l'ordine decrescente w3big_author campo.

<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT w3big_id, w3big_title, 
               w3big_author, submission_date
        FROM w3big_tbl
        ORDER BY  w3big_author DESC';

mysql_select_db('w3big');
$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['w3big_id']}  <br> ".
         "Title: {$row['w3big_title']} <br> ".
         "Author: {$row['w3big_author']} <br> ".
         "Submission Date : {$row['submission_date']} <br> ".
         "--------------------------------<br>";
} 
echo "Fetched data successfully\n";
mysql_close($conn);
?>