+ Start a Discussion
arjunkodidalasfdc1.3904276015566733E12arjunkodidalasfdc1.3904276015566733E12 

SOQL OR condition

Hi,

I need to write a soql query with AND and OR condition.  

string member='1234';
string group='6789';

Select ID from contact where firstname=:fname AND lastname:=lname AND memberid__c=:member OR group__c=:group.

I know the above query is wrong. I need to check few fields with AND  condition and few fields with OR condition. 
Can any one please correct this query.


Thanks.



Best Answer chosen by arjunkodidalasfdc1.3904276015566733E12
Sonam_SFDCSonam_SFDC
You need to use the parentheses where you think the condition is an AND/ OR - the query can be 

Select ID from contact where (firstname=:fname AND lastname:=lname) AND (memberid__c=:member OR group__c=:group)

read more: http://www.salesforce.com/us/developer/docs/soql_sosl/index.htm : Condition Expression Syntax (WHERE Clause)

All Answers

Ramu_SFDCRamu_SFDC
Try this Select ID from contact where (firstname=:fname AND lastname:=lname AND memberid__c=:member) OR group__c=:group
Sonam_SFDCSonam_SFDC
You need to use the parentheses where you think the condition is an AND/ OR - the query can be 

Select ID from contact where (firstname=:fname AND lastname:=lname) AND (memberid__c=:member OR group__c=:group)

read more: http://www.salesforce.com/us/developer/docs/soql_sosl/index.htm : Condition Expression Syntax (WHERE Clause)
This was selected as the best answer