Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a conditional WHERE clause?

Tags:

I need to have a conditional where clause that operates as so:

Select * From Table If (@booleanResult) Begin   Where Column1 = 'value1' End Else Begin   Where column1 = 'value1' and column2 = 'value2' End 

Any help would be appreciated.

like image 966
Yatrix Avatar asked May 09 '12 18:05

Yatrix


People also ask

Can we use 2 conditions in WHERE clause?

You can specify multiple conditions in a single WHERE clause to, say, retrieve rows based on the values in multiple columns. You can use the AND and OR operators to combine two or more conditions into a compound condition. AND, OR, and a third operator, NOT, are logical operators.

Which conditions can we use with WHERE clause?

The SQL WHERE clause is used to specify a condition while fetching the data from a single table or by joining with multiple tables. If the given condition is satisfied, then only it returns a specific value from the table. You should use the WHERE clause to filter the records and fetching only the necessary records.

Can we use or condition in WHERE clause in SQL?

You can use the OR condition in the WHERE clause to test multiple conditions where the record is returned if any one of the conditions are met. This example uses the WHERE clause to define multiple conditions, but instead of using the AND condition, it uses the OR condition.

What is conditional clause in SQL?

A "conditional clause" is a portion of a query that restricts the rows matched by certain conditions. In SQL, that means the WHERE or HAVING portions of a SELECT , UPDATE , or DELETE query.


1 Answers

Could you just do the following?

SELECT     * FROM     Table WHERE     (@booleanResult = 1     AND Column1 = 'value1') OR     (@booleanResult = 0     AND Column1 = 'value1'     AND Column2 = 'value2') 
like image 180
sazh Avatar answered Dec 06 '22 13:12

sazh