Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace output of SELECT statemnt in Mysql?

Tags:

sql

mysql

I have a query

SELECT id, descr Category, parent_id `Department`, updated `Updated`
     , updatedby `By`
FROM category

which returns numbers at the column parent_id. It can only return one of three number (167, 154, 164) at parent_id column. I want to replace those numbers with:

164 - Advertisemnt
167 - Modeling
154 - Finance 

How can that be achieved? so , the table should look like:

id category Department Updated By

1  Network  Modeling   now()   Me
like image 242
MUFC Avatar asked Feb 02 '26 05:02

MUFC


1 Answers

Use as CASE statement like this:

SELECT id
      ,descr AS `Category`
      ,CASE parent_id 
         WHEN 164 THEN 'Advertisemnt'
         WHEN 167 THEN 'Modeling'
         WHEN 154 THEN 'Finance'
         ELSE          'Unknown partent_id'
       END AS `Department`
     ,updated AS `Updated`
     ,updatedby AS `By`
FROM  category
like image 118
Erwin Brandstetter Avatar answered Feb 03 '26 18:02

Erwin Brandstetter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!