SELECT * WHERE var == [众多选择之一]
SELECT * WHERE var == [one of many alternatives]
在 MySQL 中,我可以用 OR
指定替代匹配:
SELECT * FROM table WHERE var = 1 OR var = 2
当备选方案很多时,像这样一一罗列会显得很麻烦。有没有办法,我可以给出一个列表、向量或值数组?有点像这个伪代码:
SELECT * FROM table WHERE var IN {1, 2, 5, 11, 45}
您可以用圆括号而不是大括号指定 IN
。
SELECT * FROM table WHERE var IN (1, 2, 5, 11, 45)
MySQL
中的第一个 equality/comparison
运算符是 =
而不是 ==
.
是的 你可以通过引入 IN
子句来达到同样的目的。但是您的语法不符合 MySQL
标准。应该是:
SELECT * FROM table WHERE your_column IN (1,2,5,11,45)
注:
Use IN.
IN will use an index.
OR will (afaik) not use an index.
Also, and this point is not to be sneezed at, the IN version:
- uses less code
- is easier to maintain
- is easier to understand
For those reasons alone I would be prepared suffer a little
performance to gain code quality, but you actually gain performance
too.
OR VS IN
在 MySQL 中,我可以用 OR
指定替代匹配:
SELECT * FROM table WHERE var = 1 OR var = 2
当备选方案很多时,像这样一一罗列会显得很麻烦。有没有办法,我可以给出一个列表、向量或值数组?有点像这个伪代码:
SELECT * FROM table WHERE var IN {1, 2, 5, 11, 45}
您可以用圆括号而不是大括号指定 IN
。
SELECT * FROM table WHERE var IN (1, 2, 5, 11, 45)
MySQL
中的第一个 equality/comparison
运算符是 =
而不是 ==
.
是的 你可以通过引入 IN
子句来达到同样的目的。但是您的语法不符合 MySQL
标准。应该是:
SELECT * FROM table WHERE your_column IN (1,2,5,11,45)
注:
Use IN.
IN will use an index.
OR will (afaik) not use an index.
Also, and this point is not to be sneezed at, the IN version:
- uses less code
- is easier to maintain
- is easier to understand
For those reasons alone I would be prepared suffer a little performance to gain code quality, but you actually gain performance too.
OR VS IN