在mysql查询中更改结果

时间:2011-01-19 16:57:57

标签: mysql

我想操纵我从查询中获得的结果。

我有一组2.5米的行,并且状态有10个不同的ID。这些状态不会映射到另一个表中,但我想操纵我在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或浏览器来显示结果。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

您可以在案例陈述中内联它

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