First, create the mysql_order_by_test data table according to the following table structure. We will use examples to tell you bit by bit about the usage of MySQL order by.
ORDER BY uid ASC
Query data in the positive order of uid, that is, arrange it from small to large according to uid
ORDER BY uid DESC
Query data in reverse order according to uid, that is, arrange it from large to small according to uid
Let's take a look
SELECT * FROM mysql_order_by_test ORDER BY uid ASC
This statement is to query data in the positive order of uid, that is, arrange it from small to large according to uid
The result returned is:
1 Zhang San 1
2 Li Si 2
3 Wang Ermazi 1
Let's take a look
SELECT * FROM mysql_order_by_test ORDER BY uid DESC
This statement is to query data in reverse order according to uid, that is, arrange it from large to small according to uid
The returned result is:
3 Wang Ermazi 1
2 Li Si 2
1 Zhang San 1
SQL creation 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 San', 1);
INSERT INTO mysql_order_by_test (uid, name, sex) VALUES(2, 'Li Si', 2);
INSERT INTO mysql_order_by_test (uid, name, sex) VALUES(3, 'Wang Ermazi', 1);
ORDER BY uid ASC
Query data in the positive order of uid, that is, arrange it from small to large according to uid
ORDER BY uid DESC
Query data in reverse order according to uid, that is, arrange it from large to small according to uid
Let's take a look
SELECT * FROM mysql_order_by_test ORDER BY uid ASC
This statement is to query data in the positive order of uid, that is, arrange it from small to large according to uid
The result returned is:
1 Zhang San 1
2 Li Si 2
3 Wang Ermazi 1
Let's take a look
SELECT * FROM mysql_order_by_test ORDER BY uid DESC
This statement is to query data in reverse order according to uid, that is, arrange it from large to small according to uid
The returned result is:
3 Wang Ermazi 1
2 Li Si 2
1 Zhang San 1
SQL creation code:
Copy the codeThe code is as follows:
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 San', 1);
INSERT INTO mysql_order_by_test (uid, name, sex) VALUES(2, 'Li Si', 2);
INSERT INTO mysql_order_by_test (uid, name, sex) VALUES(3, 'Wang Ermazi', 1);