0
searchcon = [select id,MailingPostalCode,Name,Phone 
             From Contact 
             where Name = :string1 
             or MailingPostalCode = :string2 
             limit 10];

from workbench i am doing like this SELECT Name, Phone, MailingPostalCode FROM Contact WHERE (name = 'zqaswed' OR MailingPostalCode = '')

I want to query only the contact with the name zqaswed,but my query is getting all the records with the empty zipcode,pleAse try to solve to problem.

  • Remove OR MailingPostalCode = '' from your query. If you want to show records with name = 'zqaswed' and also has postalcode as blank then use SELECT Name, Phone, MailingPostalCode FROM Contact WHERE (name = 'zqaswed' AND MailingPostalCode = '') – Prady Mar 17 '16 at 7:23
1

I am not sure what you want to do in your query

If you just want to show records only with name = 'zqaswed' then remove OR MailingPostalCode = '' from your query.

SELECT Name, Phone, MailingPostalCode FROM Contact WHERE (name = 'zqaswed' )

If you want to show records with name = 'zqaswed' and also has postalcode as blank then use

SELECT Name, Phone, MailingPostalCode FROM Contact WHERE (name = 'zqaswed' AND MailingPostalCode = '')

OR Clause checks if any of the conditions matches that is the reason why you are getting all records which have the postal code as blank Or has the name zqaswed

0

In the WHERE clause of your query you are using

name = 'zqaswed' OR MailingPostalCode = ''

The logical equivalent of that is -> Bring me all the contact records whose name is equal to 'zqaswed' , Also being me all the contact records whose mailingpostalcode is empty.

Please note that you have used the OR Clause, so the system brings you all the contact records which satisfy either of the conditions i.e.

  1. Name equals 'zqaswed'
  2. MailingPostalCode is empty

Now if you intend to fetch those contacts that satisfy both these conditions i.e. - The contacts with name 'zqaswed' and empty mailingPostalCode, then use the AND clause

Your query should be

SELECT Name, Phone, MailingPostalCode FROM Contact WHERE (name = 'zqaswed' AND MailingPostalCode = '')

Hope this helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.