Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: SQL Server
Expertise: Beginner
Sep 11, 2000

Problem with Using the AND Condition

Question:
In the WHERE condition, I have not been able to use the AND condition when the two conditions on either side of the AND command relate to data the same column.

For instance, print out all trips made to New York and San Francisco. The query returns blank data.

Can you help me?

Answer:
As you haven't attached the actual SQL you are using, I will have to make an educated guess based on your question.

Let's assume that you have a table named "trips" with a field called "destination." It sounds like you might have literally translated your English statement into something like this:

select * from trips
where
destination = 'NEW YORK'  and
destination = 'SAN FRANCISCO'
However, SQL follows the rules of Boolean logic, not English. The above statement actually tells the server only to return rows where the destination is 'NEW YORK' and 'SAN FRANCISCO'. That is an impossibility.

Instead, rephrase your request as such. You want to return all rows where the destination is either 'NEW YORK' or 'SAN FRANCISCO'. In SQL that would be:

select * from trips 
where
destination = 'NEW YORK' or
destination = 'SAN FRANCISCO'
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date