Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the T-SQL Syntax for If This or This then That else Do Nothing?

Tags:

sql

sql-server

I am trying to make a condition where for a certain ID, when either of two values from two different tables are greater than a number, then I will display a row with both values. Otherwise, I don't want to display any new row. What is the correct syntax for this?

if(select 
       a.Column1 > 2 or 
       b.Column2 > 2 
   from 
       Table1 a join Table2 b on a.ID = b.ID) 
begin 
select 
    a.Column1, 
    b.Column2 
from 
    Table1 a join Table2 b on a.ID = b.ID) 
end

else 
begin 
    Don't Select
end
like image 428
Fal Avatar asked Dec 21 '25 23:12

Fal


1 Answers

You just need to add it as a where condition. If your where condition fails for a given row, that row wouldn't be selected.

select 
    a.Column1, 
    b.Column2 
from 
    Table1 a join Table2 b on a.ID = b.ID
where a.column1 > 2 or b.column2 > 2
like image 66
Vamsi Prabhala Avatar answered Dec 23 '25 22:12

Vamsi Prabhala