tags:

views:

27

answers:

2

I have a problem with the following MySQL query:

SELECT * FROM TPDSessions  WHERE  TPDS_Morning = 0 AND TPDD_Id = 1 AND TPDS_Chair1_idPerson = 16785 OR TPDS_Chair2_idPerson = 16785

The row returned has TPDS_Morning with a value of 1 in it, and I don't get why. This is the table structure:

CREATE TABLE `TPDSessions` (
  `TPDS_Id` int(11) NOT NULL auto_increment,
 `TPDR_Id` int(32) NOT NULL,
 `TPDD_Id` int(32) NOT NULL,
 `TPDS_Morning` tinyint(1) NOT NULL,
 `TPDS_Name` varchar(200) NOT NULL,
 `TPDS_Chair1_idPerson` int(11) NOT NULL,
`TPDS_Chair2_idPerson` int(11) NOT NULL,   
 PRIMARY KEY  (`TPDS_Id`), 
KEY `chair1_idPerson` (`TPDS_Chair1_idPerson`,`TPDS_Chair2_idPerson`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;

If anyone can explain how to adjust my query to not return that result...

+2  A: 

You might be missing some brackets with the OR statement.

It could be either

SELECT  * 
FROM    TPDSessions  
WHERE   (TPDS_Morning = 0 
AND     TPDD_Id = 1 
AND     TPDS_Chair1_idPerson = 16785) 
OR      TPDS_Chair2_idPerson = 16785

SELECT  * 
FROM    TPDSessions  
WHERE   TPDS_Morning = 0 
AND     TPDD_Id = 1 
AND     (TPDS_Chair1_idPerson = 16785 
OR      TPDS_Chair2_idPerson = 16785)

I think the second is more likely what you are after.

astander
Yes, given the question the second one is very likely
Vinko Vrsalovic
argh..yes that's it. Thanks for clearing that up, had this problem before but not often enough apparently to notice it this time. Thanks!
Maarten
+1  A: 

Because you have an OR at the end. There could be a row with Chair2_idPerson = 16875 and with TDPS_Morning = 1. Place brackets where appropriate to group the statements.

Vinko Vrsalovic