1. sql語句查詢:按照指定值排序
SELECT * FROM meter.`t_price`
ORDER BY
CASE t_status //選擇排序排序
WHEN 2 THEN 1 # 當前值為2,顯示為1
WHEN 1 THEN 2 # 當前值為1,顯示為2
WHEN 3 THEN 3 # 當前值為3,顯示為3
END
ASC -- 按正序排序;DESC倒序排列
2. case用法
用法1:簡單case函數
case 列名 //選擇排序
when 條件值1 then 選項1
when 條件值2 then 選項2
……
[else 默認值] # else 默認值,非必須的
end
用法2:case搜索函數
# 用法2:case搜索函數
case
when 列名=條件值1 then 選項1
when 列名=條件值2 then 選項2
……
[else 默認值(選項0)] # else 默認值,非必須的
end
實例
UPDATE meter.`t_price`
SET t_order =
CASE
WHEN t_status=2 THEN 1 # t_status為2,t_order修改1
WHEN t_status= 1 THEN 2 # t_status為1,t_order修改2
WHEN t_status=3 THEN 3 # t_status為3,t_order修改3
END