devxlogo

Problem with Using the AND Condition

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 tripswheredestination = 'NEW YORK'  anddestination = '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 wheredestination = 'NEW YORK' ordestination = 'SAN FRANCISCO'

devx-admin

Share the Post: