gpt4 book ai didi

mysql - mysql 语句中的 ORDER BY CASE

转载 作者:行者123 更新时间:2023-11-29 22:27:33 25 4
gpt4 key购买 nike

我经常在论坛上查看这个问题,但由于我只是一个初学者,我无法找到可以用我的一点知识用于我的代码工作的答案。这就是我想要解决的问题:

我的商店里有很多产品。有 4 种不同类型。

  1. 产品名称以“AA-”开头
  2. 产品名称以“BB-”开头
  3. 产品名称以“CC-”开头
  4. 产品名称以“DF-”开头

不,当我向查看者展示产品时,我想使用 ORDER BY ASC。

但是对于产品“CC-”和“DF-”,我希望将其订购为 DESC。

如果我的产品名称以“BB-”开头,则应按价格排序(也在 select 语句中调用)

我了解到我应该为此使用 CASE。但我不知 Prop 体是怎样的。

最佳答案

我认为在这种情况下你也可以轻松地使用UNION,如下所示:

select t1.product_name Name, t1.product_price Price from

(select product_name, product_price from shop
where product_name like 'cc%' or product_name like 'de%'
order by product_name desc) t1

union

select t2.* from
( select product_name, product_price from shop
where product_name like 'bb%'
order by product_price ) t2

union

select t3.* from
( select product_name, product_price from shop
where ( product_name not like 'cc%') and ( product_name not like 'de%')
and (product_name not like 'bb%')
order by product_name, product_price) t3 ;

关于mysql - mysql 语句中的 ORDER BY CASE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30143577/

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