Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-01-2012, 01:29 AM   PM User | #1
sonny
Regular Coder

 
sonny's Avatar
 
Join Date: Apr 2008
Location: United States
Posts: 567
Thanks: 88
Thanked 0 Times in 0 Posts
sonny can only hope to improve
query syntax and conditions

Hi
just curious, about a few things, I am going over my query's
and changing || to OR adding brackets, stuff like that.

I know its best to enclose different conditions from reading up
whether you need too or not, separating "OR, AND" etc, just curious
should I put both in brackets when they exist? or one over another?

also should I always do AND conditions in the beginning no matter what?
or are they ok at the end if using brackets?

also is it good practice to use single quotes like '$var' in query, double or
none at all.

I'm strictly doing query's inside scripts only.

Thanks as always
Sonny
sonny is offline   Reply With Quote
Old 12-01-2012, 07:50 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,567
Thanks: 62
Thanked 4,057 Times in 4,026 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Too general a question. Depends entirely upon what the WHERE condition is supposed to be testing.

Code:
WHERE age >= 18 OR age >= 16 AND parentalConsent = True
There is an AND condition at the end with no parentheses at all, yet the meaning is perfectly clear:

It means the same as
Code:
WHERE age >= 18 OR ( age >= 16 AND parentalConsent = True )
*clearly* it would be a mistake to use
Code:
WHERE ( age >= 18 OR age >= 16 ) AND parentalConsent = True
When in doubt, probably use them. But use them correctly.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Users who have thanked Old Pedant for this post:
sonny (12-01-2012)
Old 12-01-2012, 08:04 AM   PM User | #3
sonny
Regular Coder

 
sonny's Avatar
 
Join Date: Apr 2008
Location: United States
Posts: 567
Thanks: 88
Thanked 0 Times in 0 Posts
sonny can only hope to improve
I did notice a weird result now and then but when I went back and
added some brackets, that fixed things. I thought using LIKE let me
have a space before or after a word, without using that %, I really
do not like using them if I can, I will pay more attention and use =
whenever I can thanks

PS "NOT IN" is another thing I need to go back and use now. its
efficient, I like that.

Thanks
Sonny
sonny is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:44 PM.


Advertisement
Log in to turn off these ads.