Change results in mysql query

I would like to process the result obtained from the request.

I have a set of 2.5 m lines, and for status there are 10 different identifiers. These statuses do not appear in another table, but I would like to process the result that I get in 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 a request? Please note that I do not use PHP or a browser to display the results.

+4
source share
2 answers
 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 
+4
source

You can insert 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 highly recommend that you create a lookup table for this

Tbl Status contains 2 columns ID and Description

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

Source: https://habr.com/ru/post/1336175/


All Articles