HOME | PHP Tutorial | CSS
facebook icon
AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP
HTML Tutorial

CSS Tutorial

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
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
SELECT * FROM Name
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

2819. SQL Tutorial
Gives you clear, concise and easy to understand SQL tutorial with practical examples. You will learn how to write SQL query like a Pro.
http://www.zentut.com/sql-tutorial/

2589. SQL WHERE - SQL Tutorial

http://www.sql-tutorial.net/SQL-WHERE.asp

2568. SQL COUNT - SQL Tutorial

http://www.sql-tutorial.net/SQL-COUNT.asp

2578. SQL LIKE - SQL Tutorial

http://www.sql-tutorial.net/SQL-LIKE.asp

2583. SQL SELECT INTO - SQL Tutorial

http://www.sql-tutorial.net/SQL-SELECT-INTO.asp

3294. SQL WHERE AND & OR multiple conditions, sql tutorial
SQL WHERE AND & OR multiple conditions sql tutorial
https://www.agernic.com/sql-tutorial/sql-where-and-or-multiple-conditions.html

Your add here
How to get PHP errors to display?
How to prevent sql injection in php mysqli
How do I get PHP errors to display - PHP error_reporting()
mysql_query(): The mysql extension is deprecated and will be removed i
Function eregi_replace() is deprecated in php - How to fix
PHP delete an element from an array
Uncaught Error: Call to undefined function MYSQL_NUM_ROWS() in
Deprecated: mysql_connect() mysql extension is deprecated and will be
mysqli_fetch_array() expects parameter 1 to be mysqli_result boolean g
SQL IN Operator - example
SQL BETWEEN Operator
SQL like syntax and example
SQL WHERE AND & OR multiple conditions
SQL create table primary key autoincrement
PHP mysqli_connect () Function
SQL COUNT(), AVG() and SUM() Functions
Display data from SQL database with MySQLi into php - html table
SQL CREATE DATABASE - example
SQL mysql order by columns, by date, by asc, by desc, random with PHP
PHP mysqli_query() Function - mysqli query
SQL delete row - SQL delete table
SQL update - SQL update select
Insert into SQL - Insert into table SQL - Insert data in sql
Login in PHP - simple login script with PHP MYSQL