gpt4 book ai didi

mysql - 按条件排序到字段

转载 作者:行者123 更新时间:2023-11-29 01:16:23 26 4
gpt4 key购买 nike

我想改变字段函数中的order by子句,但我不知道这是否可能。

这是一个例子:

**TABLE DISCOUNTS**
category offer
A 0
B 0
C 1
D 1
F 0

product_id category price
1 A 100
2 B 300
3 C 250
4 C 150
5 D 200
6 B 200
7 F 250

我愿意选择所有但已订购的产品,首先是它们是否是报价(我想先显示报价),然后我想按类别对它们进行分组并按价格排序。

问题出在优惠上,这里按类别分组并不重要,因为优惠不会太多,直接按价格排序更有意义。

所以这是我得到的输出:

product_id  category    price       offer
4 C 150 1
3 C 250 1
5 D 200 1
1 A 100 0
6 B 200 0
7 F 250 0
2 B 300 0

虽然我想获得不同的订单(仅按价格排序报价):

product_id  category    price       offer
4 C 150 1
5 D 200 1
3 C 250 1
1 A 100 0
6 B 200 0
2 B 300 0
7 F 250 0

我正在使用的查询是:

select * from Products p
left join Discounts d on p.category = d.category
order by offer desc, p.category, price

这里是 fiddle

这里最好的选择是什么?谢谢。

最佳答案

我认为您只需要从 order by 中删除 category:

select *
from Products p left join
Discounts d
on p.category = d.category
order by offer desc,
(case when offer = 0 then category end),
price ;

关于mysql - 按条件排序到字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30456975/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com