开发者

'AND' and 'OR' in SQL queries

开发者 https://www.devze.com 2022-12-25 03:12 出处:网络
I was hoping someone could give me a hand on how I write this correctly. $details_quey = mysql_query(\"SELECT * FROM `accounts` WHERE ONLINE开发者_运维知识库 = \'true\' OR FORCE_AWAY = \'true\' OR AW

I was hoping someone could give me a hand on how I write this correctly.

$details_quey = mysql_query("SELECT * FROM `accounts` WHERE ONLINE开发者_运维知识库 = 'true' OR FORCE_AWAY = 'true' OR AWAY = 'true' AND FORCE_OFFLINE = 'false' AND TOKEN = '$con_token' ", $CON )

Any help much appreciated.


SELECT * FROM `accounts` 
WHERE 
(ONLINE = 'true' OR FORCE_AWAY = 'true' OR AWAY = 'true')
AND FORCE_OFFLINE = 'false' AND TOKEN = '$con_token' 

Guessing a bit, but I think that is what you want.

Assuming you want anything matching any one of the ORs and all of the ANDs


AND has precedence over OR in a SQL Query. Therefore, your query valuates as such right now:

SELECT * FROM `accounts` WHERE
    ONLINE = 'true' OR
    FORCE_AWAY = 'true' OR
    ( AWAY = 'true' AND 
    FORCE_OFFLINE = 'false' AND
    TOKEN = '$con_token' );

Simply use ( ) to group conditions which should be evaluated together:

SELECT * FROM `accounts` WHERE
    (ONLINE = 'true' OR
    FORCE_AWAY = 'true') OR
    (AWAY = 'true' AND 
    FORCE_OFFLINE = 'false') AND
    TOKEN = '$con_token' );


"SELECT * FROM `accounts` WHERE (ONLINE = 'true' OR FORCE_AWAY = 'true' OR AWAY = 'true') AND FORCE_OFFLINE = 'false' AND TOKEN = '$con_token' ", 

Just use some braces to keep the and or logic seperate. Check if the person is online or force away or away AND that the forceoffline is false and token value = con token.


Not sure what you're looking for, but brackets sure will help for the OR's:

$details_quey = mysql_query("SELECT * FROM `accounts` WHERE (ONLINE = 'true' OR FORCE_AWAY = 'true' OR AWAY = 'true') AND FORCE_OFFLINE = 'false' AND TOKEN = '$con_token' ", $CON )


It's hard to tell what exactly you're trying to accomplish, but assume you want:

(online = true OR force_away=true OR away=true) AND force_offline=false AND token=$con_token

Your ORs need to be grouped together in parenthesis or the ANDs won't make much sense.

SELECT * FROM `accounts` WHERE (ONLINE = 'true' OR FORCE_AWAY = 'true' OR AWAY = 'true' AND FORCE_OFFLINE = 'false') AND TOKEN = '$con_token
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号