簡體   English   中英

mysql語句中的ORDER BY CASE

[英]ORDER BY CASE in mysql statement

我已經在這個論壇上大量檢查了這個問題,但是由於我只是一個初學者,所以我在我不甚了解的情況下找不到可以用於我的代碼工作的答案。 這是我要解決的問題:

我的商店里有很多產品。 有4種不同的類型。

  1. product_name以“ AA-”開頭
  2. product_name以“ BB-”開頭
  3. product_name以“ CC-”開頭
  4. product_name以“ DF-”開頭

否,當我將產品展示給查看者時,我想使用ORDER BY ASC。

但是對於產品“ CC-”和“ DF-”,我希望訂購DESC。

如果我的product_name以“ BB-”開頭,則應按價格排序(在select語句中也稱為)

我知道我應該為此使用CASE。 但是我不知道到底是怎么回事。

我認為您在這種情況下也可以輕松使用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 ;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM