mysql - 选择语法 - 是 "something = (' this' or 'that' or 'other' )"ok to do?

标签 mysql select syntax

我正在使用 MySQL v5.5.27。我通常会这样写这个 SELECT 语句:

SELECT DISTINCT * 
FROM table 
WHERE something = 'this' 
    OR something = 'that' 
    OR something = 'other' 
    AND thingamajig = 'one' 
    OR thingamajig = 'two'

但是这个 SELECT 语句会提供完全相同的结果吗?

SELECT DISTINCT * 
FROM table 
WHERE something = ('this' OR 'that' OR 'other') 
    AND thingamajig = ('one' OR 'two')

我试过运行它并且它似乎正在运行。只是想确保第二种做事方式不会以我无法想到的某种方式返回错误数据。

感谢您的任何见解和帮助!

最佳答案

我建议使用括号来清楚地对搜索条件的顺序进行分组。

你的第二个 SELECT 语句比你的第一个更清晰。

SELECT DISTINCT * 
FROM table 
WHERE something = ('this' OR 'that' OR 'other') 
AND thingamajig = ('one' OR 'two')

请记住,当您有多个值时,您也可以使用 IN 运算符:

SELECT DISTINCT * 
FROM table 
WHERE something IN ('this', 'that', 'other') 
AND thingamajig IN ('one', 'two')

此外,使用 IN 运算符 performs much faster而不是使用 OR

关于mysql - 选择语法 - 是 "something = (' this' or 'that' or 'other' )"ok to do?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12604806/

相关文章:

jquery - jquery中如何匹配字符串

java - SELECT e From Employee e - 为什么是多余的 "e"?

ruby - 我怎样才能将双重 splat 参数折叠成空?

mysql - 选择每个优惠券价格的最大折扣百分比

mysql 服务无法在 Windows 7 上运行

javascript - 展开连接查询中的嵌套数据

java - 转换循环(Java初学者问题)

mysql - SQL如何使用减号或!=

php - 将链接数据从一个表中的一行提取到另一表中的多行

php - mysql 从 3 个表中选择,其中 2 个可能为空