简体   繁体   English

如何选择几列中的一列等于某个值的行?

[英]how to select a row where one of several columns equals a certain value?

Say I have a table that includes column A, column B and column C. How do I write I query that selects all rows where either column A OR column B OR column C equals a certain value? 假设我有一个包含A列,B列和C列的表。如何编写I查询,选择列A或列B或列C等于某个值的所有行? Thanks. 谢谢。

Update: I think forgot to mention my confusion. 更新:我想忘了提到我的困惑。 Say there is another column (column 1) and I need to select based on the following logic: 假设有另一列(第1列),我需要根据以下逻辑进行选择:

...where Column1 = '..' AND (ColumnA='..' OR ColumnB='..' OR ColumnC='..') ...其中Column1 ='..'AND(ColumnA ='..'或ColumnB ='..'或ColumnC ='..')

Is it valid to group statements as I did above with parenthesis to get the desired logic? 像上面用括号一样对语句进行分组以获得所需的逻辑是否有效?

除非我在这里遗漏了什么......

SELECT * FROM MYTABLE WHERE COLUMNA=MyValue OR COLUMNB=MyValue OR COLUMNC=MyValue

I prefer this way as its neater 我更喜欢这种方式

select *
from mytable
where
myvalue in (ColumnA, ColumnB, ColumnC)
SELECT *
FROM myTable
WHERE (Column1 = MyOtherValue) AND
      ((ColumnA = MyValue) OR (ColumnB = MyValue) OR (ColumnC = MyValue))

Yes, it's valid to use parentheses. 是的,使用括号是有效的。 However, if you're searching multiple columns for the same value, you may want to consider normalizing the database. 但是,如果要在多个列中搜索相同的值,则可能需要考虑规范化数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM