开发者

MySQL query problem with Bool

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_idP开发者_运维百科erson` 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...


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜