Page 1 of 1

Simple query, maybe not

Posted: Mon Aug 13, 2012 7:00 pm
by Alpal
Query
SELECT SUM(Team_entries.total_score)AS score, Team_entries.team_id, Team_entries.team_club, Team_entries.team_name, Team_entries.team_grade, Team_entries.total_score, Team_entries.Updated, Team_entries.Countback
FROM Team_entries
WHERE Team_entries.team_grade='BPB' OR Team_entries.team_grade='BPBS' AND team_name<>'Bye'
GROUP BY Team_entries.team_name
ORDER BY Countback ASC, score DESC

The query does what I want it to do except it will not exclude the team_name "AND team_name<>'Bye'"

Thank you in advance for any assistance you may be able to offer

Re: Simple query, maybe not

Posted: Mon Aug 13, 2012 7:23 pm
by mikosiko
use parenthesis to group your logical operators and get the proper precedence....

indirect example:
2 + 3 * 5 <> (2 + 3) * 5

mysql operators precedence
http://dev.mysql.com/doc/refman/5.0/en/ ... dence.html

Re: Simple query, maybe not

Posted: Mon Aug 13, 2012 7:33 pm
by califdon
Yes, whenever you use both ANDs and ORs in the same clause, you need to use parentheses to group them properly. Otherwise, you're very likely to get unexpected results, like you are experiencing.

Re: Simple query, maybe not

Posted: Mon Aug 13, 2012 8:20 pm
by Alpal
Thankyou, problem solved and a lesson learnt