Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If condition in select query

Tags:

postgresql

select email from mailing list

This is the query i want to use if condition. I have a column in mailing list as unsub. If this unsub is true the email should null in the selected result otherwise the email will be the selected result

like image 625
Rafiu Avatar asked May 19 '11 11:05

Rafiu


People also ask

Can we use if statement in SELECT query in SQL?

Both IIF() and CASE resolve as expressions within a SQL statement and can only be used in well-defined places. The CASE expression cannot be used to control the flow of execution of Transact-SQL statements, statement blocks, user-defined functions, and stored procedures.

Can we use SELECT statement in IF condition?

It is like a Shorthand form of CASE statement. We can conveniently use it when we need to decide between two options. There are three parts in IIF statement, first is a condition, second is a value if the condition is true and the last part is a value if the condition is false.

Can we use if condition in SQL view?

simply use a udf (User defined Function) Here you can use IF, ELSE, WHILE etc.


1 Answers

select case unsub when true then null else email end as email,....
from ...

see the docs for more examples.

like image 157
davek Avatar answered Jan 14 '23 10:01

davek