开发者

MySQL : Order by 2 fields

开发者 https://www.devze.com 2022-12-19 20:52 出处:网络
Suppose I have a list of products, and each has a price. I wish to show the most e开发者_高级运维xpensive product, and if 2 products or more tie, then I wish to order by their name. Doing this doesn\'

Suppose I have a list of products, and each has a price. I wish to show the most e开发者_高级运维xpensive product, and if 2 products or more tie, then I wish to order by their name. Doing this doesn't seem to produce the results I want:

ORDER BY cost DESC, product_name

What is the syntax which I am looking for?


This works fine:

use test;

create table products (cost decimal(15,2), product_name varchar(50));

insert into products values (14.50, 'b product');
insert into products values (14.50, 'a product');
insert into products values (15.50, 'c product');

select * from products order by cost desc, product_name

Returns:

15.50, 'c product'
14.50, 'a product'
14.50, 'b product'


where's the problem?

ORDER BY cost DESC, product_name 

will order by cost desc and then by product_name asc. What type of unexpected behaviour are you experiencing?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号