CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   MySQL (http://www.codingforums.com/forumdisplay.php?f=7)
-   -   query syntax and conditions (http://www.codingforums.com/showthread.php?t=283330)

sonny 12-01-2012 01:29 AM

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

Old Pedant 12-01-2012 07:50 AM

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.

sonny 12-01-2012 08:04 AM

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


All times are GMT +1. The time now is 05:54 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.