Skip to content
Advertisement

Show a text depending on the value of a column MySQL

I would like to know, how can I build a query that shows me an extra column where instead of showing the true value of the original column it shows me the text that i want?

For example, The Column, in this case “inputOutput”, save values ​​0 or 1, and I would like to show in a query that if the value is 0 I can see the text “Output” and if it is 1 show “Input” in all the rows.

what function or structure could you use?

This is just an example of the columns that I would like to show:

Select idInventory, date, "text that that replaces 0 or 1 from inputOtput column" 
from inventory;

Advertisement

Answer

You can use a case expression for this:

select 
    idInventory, 
    date, 
    case inputOutput 
        when 0 then 'Output'
        when 1 then 'Input'
        else '???'
    end status
from inventory;
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement