Create the mysql_order_by_test data table according to the following table structure. We will use instance to tell you how to use MySQL order.
Order by uid ASC
Query data in the forward order of uid, that is, sort data in ascending order of uid
Order by uid DESC
Query data by uid in reverse order, that is, sort data by uid in ascending order
Let's see
SELECT * FROM mysql_order_by_test order by uid ASC
This statement is used to query data in the forward order of uid, that is, sort data in ascending order of uid.
The returned result is:
1 Zhang San 1
2 Li Si 2
3 Wang Erma Zi 1
Let's see
SELECT * FROM mysql_order_by_test order by uid DESC
This statement is used to query data in reverse order by uid, that is, sort data in ascending order by uid
The returned result is:
3 Wang Erma Zi 1
2 Li Si 2 www.2cto.com
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 Erma sub', 1 );