Home > Database > Mysql Tutorial > body text

mysql order by

黄舟
Release: 2017-01-16 13:09:00
Original
1578 people have browsed it

order by
Sort: It only makes sense after the result set comes out. It must be after where, group by, having
desc (descending order)/asc (ascending order)

Use field sorting

Use shop_price to sort in descending order

select goods_name,cat_id,shop_price from goods where cat_id=4 order by shop_price desc;
Copy after login

Multiple sort selections, first based on cat_id, then shop_price

select cat_id,shop_price,goods_name from goods order by cat_id ,shop_price;
Copy after login

limit limit limit [pos, optional offset ] Quantity

Arrange in ascending order and take out the top ten

select goods_id,goods_name from goods where cat_id=3 order by shop_price asc limit 10;
Copy after login

The top five with the highest price

mysql> select goods_name ,shop_price from goods order by shop_price desc limit 0,5;
Copy after login
mysql> select goods_name ,shop_price from goods order by shop_price desc limit 5;
+----------------+------------+
| goods_name | shop_price |
+----------------+------------+
| 多普达Touch HD | 5999.00 | 
| 诺基亚N96 | 3700.00 | 
| 诺基亚N85 | 3010.00 | 
| testPhone | 3000.00 | 
| 夏新T5 | 2878.00 | 
+----------------+------------+
Copy after login

The three with the highest price starting from the third First name (or third to fifth place)

mysql> select goods_name ,shop_price from goods order by shop_price desc limit 2,3;
+------------+------------+
| goods_name | shop_price |
+------------+------------+
| 诺基亚N85 | 3010.00 | 
| testPhone | 3000.00 | 
| 夏新T5 | 2878.00 | 
+------------+------------+
Copy after login

Take out the highest priced item

mysql> select goods_name ,shop_price from goods order by shop_price desc limit 1;
+----------------+------------+
| goods_name | shop_price |
+----------------+------------+
| 多普达Touch HD | 5999.00 | 
+----------------+------------+
Copy after login

Skills: [Judge where] [Group group_by] [Filter having] [Sort order by] [Filter limit]

Get the latest products in each type

select cat_id, goods_id ,goods_name from(
(select cat_id,goods_id ,goods_name from goods order by cat_id desc,goods_id desc ) as tmp
)group by cat_id order by cat_id desc;
Copy after login
select cat_id,goods_id ,goods_name from goods where goods_id in (
select max(goods_id) from goods group by cat_id 
) order by cat_id desc;
Copy after login

The result of the query can be
Single column and single row can be used = again Filter
You can use in for single column and multiple rows. Filter again
You can use from for multiple columns and multiple rows. Filter again

The above is the content of mysql order by. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template