Mysql Select replace a code with information

Asked

Viewed 84 times

2

I have two situations in the search inside Mysql that I need to exchange the code for a literal data.

Field: Pgto (possible data: S or N)

In the search if the Pgto field is N I display on the screen No and if it is S I display Yes.

The other situation is the Status field (possible data: 1, 2, 3 and 4), in which if in the search for 1 I display Pgto Pending, if it is 2 I display Released, and so on.

How do I do this within Mysql Select.

  • Brenno, check if the case matches: https://www.w3schools.com/sql/func_mysql_case.asp

2 answers

0

Hello,

You can use the if functions of Mysql or even CASE,

SELECT Pgto,
CASE
    WHEN Pgto = 'S' THEN "Sim"
    ELSE "Não"
END
FROM a_tua_tabela;

0

You can do it this way

SELECT 
CASE 
    WHEN Pgto = 'S' THEN 'Sim'
    ELSE 'Não'
END AS Pgto,
CASE
    WHEN Status = 1 THEN 'Pendente de Pgto'
    WHEN Status = 2 THEN 'Liberado'
    WHEN Status = 3 THEN 'A caminho'
    WHEN Status = 4 THEN 'Entregue'
    ELSE 'Não definido'
END AS Status
FROM ...

With Case When you can pass N when to serve you the way you think you need.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.