Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find all records where two conditions are true

I'm trying to find all records where two conditions are true. For example:

ruby-1.8.7-p302 > Person.all
 => #<Person name: "Jane", city: "Green Bay", state: "Wisconsin", single: true>
 => #<Person name: "Dick", city: "Madison", state: "Wisconsin", single: false> 
 => #<Person name: "Tom", city: "Milwaukee", state: "Wisconsin", single: true>

I want to get the "Jane" and "Tom" records. I'm trying this, but it doesn't work:

Person.find_all_by_state("Wisconsin").find_all_by_single(true)
like image 507
sybind Avatar asked Feb 10 '11 03:02

sybind


People also ask

How can I satisfy two conditions in SQL?

The SQL AND condition and OR condition can be combined to test for multiple conditions in a SELECT, INSERT, UPDATE, or DELETE statement. When combining these conditions, it is important to use parentheses so that the database knows what order to evaluate each condition.

Can we use 2 WHERE clause in SQL?

Example - Two Conditions in the WHERE Clause (AND Condition)You can use the AND condition in the WHERE clause to specify more than 1 condition that must be met for the record to be selected.

Which operator is used to display all records if all conditions are true?

The SQL ALL Operator The ALL operator: returns a boolean value as a result. returns TRUE if ALL of the subquery values meet the condition.

Which operator is used to fetch data when any one of the multiple conditions is true?

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. Logical operators, or Boolean operators, are operators designed to work with truth values: true, false, and unknown.


1 Answers

Example using a OR condition:

model_name.where("field_1 = ? OR field_2 = ?", params[:search_string],  params[:search_string])
like image 105
Shivam Tripathi Avatar answered Nov 14 '22 14:11

Shivam Tripathi