0

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...

1

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, given the question the second one is very likely
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!
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.