MySQL Order By usage sharing

  • 2020-05-13 03:39:17
  • OfStack

Start by creating the mysql_order_by_test data table with the following table structure, and we'll tell you the usage of MySQL order by with the example 1 point 1 point.

ORDER BY uid ASC
Query the data in the positive order of uid, which is the order from small to large of uid
ORDER BY uid DESC
Query the data in reverse order according to uid, that is, from large to small according to uid

Let's take a look at
SELECT * FROM mysql_order_by_test ORDER BY uid ASC

This statement queries the data in the positive order uid, which is the order from small to large uid
The result is:

1 piece of 3 to 1
Li 4 2
King 2 pockmarked 1

Let's take a look at
SELECT * FROM mysql_order_by_test ORDER BY uid DESC

This statement queries the data in reverse order by uid, which is the order from large to small by uid

The result returned is:
King 2 pockmarked 1
Li 4 2
1 piece of 3 to 1

SQL create code:
 
CREATE TABLE IF NOT EXISTS mysql_order_by_test ( 
uid int(10) NOT NULL AUTO_INCREMENT, 
name char(80) NOT NULL, 
sex tinyint(1) NOT NULL, 
KEY uid (uid) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ; 

INSERT INTO mysql_order_by_test (uid, name, sex) VALUES(1, ' zhang 3', 1); 
INSERT INTO mysql_order_by_test (uid, name, sex) VALUES(2, ' li 4', 2); 
INSERT INTO mysql_order_by_test (uid, name, sex) VALUES(3, ' The king 2 mike ', 1); 

Related articles: