简体   繁体   中英

Mysql IF two columns equal and if they are both greater than one

I have a query that pulls users in a list. It pulls a list of people that you can "fight", You shouldn't be able to fight yourself, or fight someone in your "clan".

 $select = mysql_query("SELECT id,username,clan FROM `users` WHERE username != '".$user['username']."' AND IF clan>0 THEN clan != ".$user['clan']."");

This query "tries" to see if the selected row's clan > 0 "Means they are in a clan", then it should see if they are in the same clan as the user. IF so, skip that row. Also, it is worth nothing that if you aren't in a clan, you should be able to fight others also not in a clan (0).

  • Can't fight yourself
  • Or anyone in your clan

Thanks :)

Try this:

SELECT id,username,clan 
FROM `users` 
WHERE username != '".$user['username']."' 
AND (clan = 0 OR clan != ".$user['clan'].")"
SELECT id,username,clan 
FROM `users` 
WHERE username != '".$user['username']."' 
  AND (
    (clan != ".$user['clan'].") OR
    (clan = 0 AND ".$user['clan']." = 0)
  )

If clanny can't fight clanless, just add check for clan=0 in first part of OR and so on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM