Match multiple columns with same value SQL

前端 未结 2 504
我在风中等你
我在风中等你 2021-01-19 23:47

Suppose the table has columns like akey1 , bkey2 , ckey3 and many more like it.
Is there way to search for a common value

2条回答
  •  说谎
    说谎 (楼主)
    2021-01-19 23:50

    Short of dynamic sql, you will have to spell out each of the column names. But you can get a bit of syntactic shortcut and only list the constant once:

    SELECT * FROM table WHERE 'xyz' IN (akey1, bkey2, ckey3)
    

    With dynamic sql, you still have to issue this same query... but you can at least use string tools to build it up first, and if you want to use wildcard matching you can look in the information_schema.columns view to find them. However, that involves opening and iterating over a cursor or returning the column data to the client, either of which involves more work than just listing out column names in the original query. Hopefully you know your database at least that well before you start issues queries to it.

提交回复
热议问题