在mysql查询中更改结果

在mysql查询中更改结果

问题描述:

我想操纵从查询中得到的结果。在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或浏览器来显示结果。

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

您可以内嵌它在一个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