Skip to content
Advertisement

SQL Query return extra column depending of condition

I want to make a query which will add a column depending of what condition is meet(I have tried with sequelize without obtaining the result that I wanted) therefore I want to try with plain SQL because It does not have as many limitations.

EXAMPLE: SELECT * FROM db.messages WHERE user_id = userID OR $contacts.user_id$ = userID

If the first condition is meet I want to add a column like:

SELECT *,'type' as 'Inbox' FROM db.messages

Second condition:

SELECT *,'type' as 'Send' FROM db.messages

I have not manage to implement it with a CASE...THEN

I would appreciette any comment.

SQL CASE STatement:

Advertisement

Answer

The syntax for the case statement seems to be invalid, check the documentation. Instead of this:

Try this:

or even simpler:

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement