SQL mysql order by columns, by date, by asc, by desc, random with PHP
The SQL ORDER BY is used to sort the data in ascending or descending order from database, based on one or more columns.
The ORDER BY keyword sorts the records in ascending order by defaultin database. If you want to sort the records in descending order, use the DESC keyword.
SQL mysql order by columns asc or desc - example
Syntax: The basic syntax of the mysqli order by;
SELECT col1, col2, col3, ...
FROM name_table
ORDER BY col1, col2, col3,... ASC|DESC;
Example 1:
Your database named as Customers
The following -SQL order by - select all - Name -from "Customers" table, sorted by the "City" column:
The result
ID | Name | Contact | Address | City |
---|---|---|---|---|
1 |
Jones Cruise | Ana Maria | Will Str. 57 | Paris |
2 | Robert Caine | Ana Trujillo | Avda. Larga 24 | Roma |
3 | Brad Bale | Antonio mauro | Mataderos 19 | Madrid |
The following -SQL order by - select all - Name -from "Customers" table, sorted by the "City" column:
SELECT * FROM Name
ORDER BY City;
The result
ID | Name | Contact | Address | City |
---|---|---|---|---|
3 | Brad Bale | Antonio mauro | Mataderos 19 | Madrid |
1 |
Jones Cruise | Ana Maria | Will Str. 57 | Paris |
2 | Robert Caine | Ana Trujillo | Avda. Larga 24 | Roma |
Note: The ORDER BY keyword sorts the records in ascending order by default
SQL ORDER BY DESC Example - How to create
The following SQL statement selects all Name from the "Customers" table, sorted DESCENDING by the "City" column:
Example 2:
welcome.php
The results -
SELECT * FROM Name
ORDER BY City DESC;
ORDER BY City DESC;
The results -
ID | Name | Contact | Address | City |
---|---|---|---|---|
2 | Robert Caine | Ana Trujillo | Avda. Larga 24 | Roma |
1 |
Jones Cruise | Ana Maria | Will Str. 57 | Paris |
3 | Brad Bale | Antonio mauro | Mataderos 19 | Madrid |
mysql order by, mysql select query php, mysql order by 2 columns, by date, by multiple columns, by random, by asc, by desc
SQL mysql order by columns, by date, by asc, by desc, random with PHP - sql tutorial
Online Editor
This tool makes it easy to create, adjust, and experiment with custom colors for the web.
HTML Templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.
CSS HTML Layout
Find here examples of creative and unique website layouts.
Free CSS HTML Menu
Find here examples of creative and unique website CSS HTML menu.