작성일 : 15-03-19 17:11
|
[MySQL] order by 순서를 개별로 강제변경
|
|
|
글쓴이 :
조형래
 조회 : 3,210
|
type 필드에 가 나 다 라 마 바 가 있다고 가정하면
order by `type` 하면
가,나,다,라,마,바
를
다,나,가,마,라,바
와 같은 이런식으로 정열하고 싶다.
---------
다음과 같이 CASE문으로 원하는 문자를 숫자(또는 문자)로 변경하시면 됩니다.
ORDER BY CASE WHEN 컬럼명 = '다' THEN 1
WHEN 컬럼명 = '나' THEN 2
WHEN 컬럼명 = '가' THEN 3
WHEN 컬럼명 = '마' THEN 4
WHEN 컬럼명 = '라' THEN 5
ELSE 6 END
// sample
$orderby_cookie_orderby = "ORDER BY CASE ";
for ($i=1; $i<count($mart_int_cookie_arr); $i++){
$orderby_cookie_orderby = $orderby_cookie_orderby." WHEN mart_int = '".$mart_int_cookie_arr[$i]."' THEN ".$i;
};
$orderby_cookie_orderby = $orderby_cookie_orderby." ELSE ".count($mart_int_cookie_arr)." END ";
|
|