MySQL SELECT WHERE EQUALS syntax

大憨熊 提交于 2021-01-27 11:47:17

问题


I have a MySQL database table called Participant that looks something like this:

(idParticipant) - (firstName) - (secondName) - (gender) - (dob)

118   John     Dunne         m    1944-04-01

117   Mary     Delaney       f    1955-05-03

116   Adam     Bermingham    m    1920-01-01

115   Eamonn   Reilly        m    1987-03-19

114   Aaron    Duane         m    1990-07-08

119   Sarah    Calvin        f    1977-07-17

When I use this query:

SELECT * FROM `Participant` WHERE idParticipant = 118 OR 119;

I think I should get the following result:

118  John    Dunne       m    1944-04-01

119  Sarah   Calvin      f    1977-07-17

But instead it just returns the whole table. Where am I going wrong in my MySQL syntax?


回答1:


You need to use WHERE idParticipant IN (118, 119);

My guess is that MySQL is implicitly converting the value of 119 to a Boolean true value, so you are saying: WHERE idParticipant = 118 OR TRUE;, thus including all the rows. The equality is evaluated first, followed by the Boolean OR.




回答2:


You should look at the statement after the WHERE as a boolean statement on its own. I mean, your expression should look like this:

SELECT * FROM `Participant` WHERE idParticipant = 118 OR idParticipant = 119;


来源:https://stackoverflow.com/questions/11036422/mysql-select-where-equals-syntax

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!