Searching Multiple Columns with Multiple Values SQL

人走茶凉 提交于 2019-12-07 22:53:35

问题


I know it is posible to serach multiple columns with one value.

I would like to serach 3-4 columns for 4 maybe 5 values

I want to check if any of my choosen columns have a certain value in them.

Example

Column 1 | Column 2 | Column 3 | Column 4 
         |          |          |
Hello    |          |          |            = True
         |          |          |
         | Goodbye  |          |            = True
         |          |  Hello   | Goodbye    = True
         |          |          |
         | Hello    |          |            = True
         |          |          |
         |          |  Goodbye |            = True

In the example I would like SQL to pull the data from all of the lines that have Hello or Goodbye even both in some cases.

Is there a way to do what I want?


回答1:


There is one more way...

SELECT *
FROM TableName
WHERE 'Value1' IN (Col1,Col2,Col3...) OR 'Val2' in (Col1,Col2,Col3...) OR ...



回答2:


If it's only 3 or 4 columns, the simplest solution would be something like this:

SELECT *
FROM TableName
WHERE Column1 IN('Hello', 'Goodbye')
OR Column2 IN('Hello', 'Goodbye')
OR Column3 IN('Hello', 'Goodbye')
OR Column4 IN('Hello', 'Goodbye')


来源:https://stackoverflow.com/questions/42821476/searching-multiple-columns-with-multiple-values-sql

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