Simple query, maybe not

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Alpal
Forum Commoner
Posts: 39
Joined: Mon Jul 26, 2010 4:08 am

Simple query, maybe not

Post 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
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Simple query, maybe not

Post 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
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Simple query, maybe not

Post 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.
Alpal
Forum Commoner
Posts: 39
Joined: Mon Jul 26, 2010 4:08 am

Re: Simple query, maybe not

Post by Alpal »

Thankyou, problem solved and a lesson learnt
Post Reply