Can I use OR in SELECT?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
SmokyBarnable
Forum Contributor
Posts: 105
Joined: Wed Nov 01, 2006 5:44 pm

Can I use OR in SELECT?

Post by SmokyBarnable »

Is it possible to select from a table using or? For example if I have a form that asks for the customers email address or user id, can I create a query such as: SELECT A OR B FROM table... Is this syntax allowed?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

User avatar
SmokyBarnable
Forum Contributor
Posts: 105
Joined: Wed Nov 01, 2006 5:44 pm

Post by SmokyBarnable »

Could I use or in WHERE?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

try it first, ask later
User avatar
SmokyBarnable
Forum Contributor
Posts: 105
Joined: Wed Nov 01, 2006 5:44 pm

Post by SmokyBarnable »

What is more efficient..using or in SELECT or using or in WHERE?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I don't understand your question. The SELECT clause and the WHERE clause are two different parts of the query. In your case, you'd put it in the WHERE clause.
User avatar
SmokyBarnable
Forum Contributor
Posts: 105
Joined: Wed Nov 01, 2006 5:44 pm

Post by SmokyBarnable »

I did some research and discovered I need to consider precedence in the WHERE clause. I tried using parenthesis however when
testing it only works if person enters their email. When they enter their buyer_id it doesn't find the row.

Code: Select all

$buyersql = "SELECT DISTINCT (buyer_id), buyer_name, buyer_email, 
	buyer_countrycode, buyer_land, buyer_zip, buyer_city, buyer_street 
	FROM auction_details 
	WHERE auction_details.order_number =0 
	AND auction_details.email_sent =0
	AND (auction_details.buyer_email='".$email."' OR auction_details.buyer_id='".$email."')";
	$mybuyer = tep_db_query($buyersql);
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I dont think $email belongs in the id column.
Post Reply