2011-01-19 84 views
4

我想操縱從查詢中得到的結果。在mysql查詢中更改結果

我有一套2.5米的行,有10個不同的身份證件。這些狀態未映射到另一個表中,但我想操縱SQLyog中獲得的結果。

What I would like to do is: 

Count(Id) | Status 
------------------ 
500.000 | 1 
750.000 | 2 

convert into a result 

Count(Id) | Status 
------------------- 
500.000 | Initial order 
750.000 | Cancelled 

這可以在查詢中完成嗎?請注意,我沒有使用PHP或瀏覽器來顯示結果。

回答

4
select 
     count(*) as TotalRecs, 
     case status 
     when 1 then "Initial Order" 
     when 2 then "Cancelled " 
     when 3 then "whatever  " 
     else  "all others " 
     end case as WordStatus 
    from 
     YourTable 
    group by 
     2 
+0

謝謝!我會稍後嘗試! +1 – Ben 2011-01-19 17:52:53

+0

'end case`應該在`end case中作爲wordstatus`部分的`end`嗎?我不記得CASE ... END,在END後有一個'CASE'。 '2 by group`也不應該工作 – RichardTheKiwi 2011-01-19 20:22:04

0

您可以內嵌它在一個case語句

select COUNT(id), 
    case status 
    when 1 then 'initial order' 
    when 2 then 'cancelled' 
    # without an else, the rest go to NULL 
    end status 
from tbl 
group by status # yes, just on status 

或者,我會強烈建議您創建一個參考表,此

TBL Status包含2列IDDescription

select COUNT(tbl.id), status.description 
from tbl 
LEFT join status on status.id = tbl.status 
group by status.description