简体   繁体   English

在mysql查询中更改结果

[英]Change results in mysql query

I would like to manipulate the result I get from a query. 我想操纵从查询中得到的结果。

I have a set of 2.5m rows and there are 10 different ID's for a status. 我有一组2.5米的行,有10个不同的ID用于状态。 These statusses are not mapped in another table but I would like to manipulate the result I get in SQLyog. 这些状态不会映射到另一个表中,但我想操纵我在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

Can this be done in the query? 可以在查询中完成吗? Note that I'm not using PHP or a browser to display the results. 请注意,我没有使用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

You can either inline it in a case statement 您可以在案例陈述中内联它

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

Or I would strongly encourage you to create a reference table for this 或者我强烈建议您为此创建一个参考表

Tbl Status contains 2 columns ID and Description Tbl Status包含2列IDDescription

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM