If I have multiple OR conditions in a SQL query that checks column values, how can I return the columns that met the condtion?

huangapple go评论65阅读模式
英文:

If I have multiple OR conditions in a SQL query that checks column values, how can I return the columns that met the condtion?

问题

Goods_Services_String 包含 "tokyo" 或 Input_Materials_String 包含 "tokyo" 或 Property_Plant_Equipment_String 包含 "tokyo" 或 Production_Process_String 包含 "tokyo" 或 Capital_Expenditures_String 包含 "tokyo" 或 Employee_Locations_String 包含 "tokyo" 或 Contractors_Locations_String 包含 "tokyo" 或 Research_And_Development_Desc_String 包含 "tokyo" 或 Distribution_String 包含 "tokyo" 或 Sales_And_Marketing_Desc_String 包含 "tokyo"。而不仅仅返回符合条件的行,我还想知道哪些列满足了这个条件。这可行吗?

英文:

Let's say I want to check 10 different columns to see if they contain the word "tokyo".

  Goods_Services_String LIKE '%tokyo%' OR 
  Input_Materials_String LIKE '%tokyo%' OR 
  Property_Plant_Equipment_String LIKE '%tokyo%' OR 
  Production_Process_String LIKE '%tokyo%' OR 
  Capital_Expenditures_String LIKE '%tokyo%' OR 
  Employee_Locations_String LIKE '%tokyo%' OR 
  Contractors_Locations_String LIKE '%tokyo%' OR 
  Research_And_Development_Desc_String LIKE '%tokyo%' OR 
  Distribution_String LIKE '%tokyo%' OR 
  Sales_And_Marketing_Desc_String LIKE '%tokyo%'

But rather than just returning the row that meets this condition, I would also like to know which of the columns met this condition. Is this possible?

答案1

得分: 2

我会使用侧向连接来处理这个。例如,在 SQL Server 中,您可以这样做:

select *
from t
cross apply (values
    ('col 1', col1),
    ('col 2', col2)
) as columns(name, value)
where value like '%tokyo%'

这是一种基于集合的方法;性能可能不会很好,但使用多个 or ... like 也不是最佳选择。

英文:

I would use a lateral join for this. E.g. in SQL server you would:

select *
from t
cross apply (values
    ('col 1', col1),
    ('col 2', col2)
) as columns(name, value)
where value like '%tokyo%'

It is a set based approach; the performance won't be good but using several or ... like isn't optimal either.

答案2

得分: 1

CASE语句是SQL处理if/then逻辑的方式。CASE语句后面至少跟随一个WHEN和THEN语句。您的SQL查询将类似于以下内容:

SELECT *,
    CASE WHEN Goods_Services_String LIKE '%tokyo%' THEN 'Goods_Services_String'
         WHEN Input_Materials_String LIKE '%tokyo%' THEN 'Input_Materials_String'
         WHEN Property_Plant_Equipment_String LIKE '%tokyo%' THEN 'Property_Plant_Equipment_String'
         WHEN Production_Process_String LIKE '%tokyo%' THEN 'Production_Process_String'
         WHEN Capital_Expenditures_String LIKE '%tokyo%' THEN 'Capital_Expenditures_String'
         WHEN Employee_Locations_String LIKE '%tokyo%' THEN 'Employee_Locations_String'
         WHEN Contractors_Locations_String LIKE '%tokyo%' THEN 'Contractors_Locations_String'
         WHEN Research_And_Development_Desc_String LIKE '%tokyo%' THEN 'Research_And_Development_Desc_String'
         WHEN Distribution_String LIKE '%tokyo%' THEN 'Distribution_String'
         WHEN Sales_And_Marketing_Desc_String LIKE '%tokyo%' THEN 'Sales_And_Marketing_Desc_String'
      ELSE NULL END AS 'tokyo was here!'
   FROM YourTableName
       WHERE Goods_Services_String LIKE '%tokyo%' OR 
             Input_Materials_String LIKE '%tokyo%' OR 
             Property_Plant_Equipment_String LIKE '%tokyo%' OR 
             Production_Process_String LIKE '%tokyo%' OR 
             Capital_Expenditures_String LIKE '%tokyo%' OR 
             Employee_Locations_String LIKE '%tokyo%' OR 
             Contractors_Locations_String LIKE '%tokyo%' OR 
             Research_And_Development_Desc_String LIKE '%tokyo%' OR 
             Distribution_String LIKE '%tokyo%' OR 
             Sales_And_Marketing_Desc_String LIKE '%tokyo%';
英文:

The CASE statement is SQL's way of handling if/then logic. The CASE statement is followed by at least one pair of WHEN and THEN statements
Your SQL query will be like Something like this:

SELECT *,
    CASE WHEN Goods_Services_String LIKE '%tokyo%' THEN 'Goods_Services_String'
         WHEN Input_Materials_String LIKE '%tokyo%' THEN 'Input_Materials_String'
         WHEN Property_Plant_Equipment_String LIKE '%tokyo%' THEN 'Property_Plant_Equipment_String'
         WHEN Production_Process_String LIKE '%tokyo%' THEN 'Production_Process_String'
         WHEN Capital_Expenditures_String LIKE '%tokyo%' THEN 'Capital_Expenditures_String'
         WHEN Employee_Locations_String LIKE '%tokyo%' THEN 'Employee_Locations_String'
         WHEN Contractors_Locations_String LIKE '%tokyo%' THEN 'Contractors_Locations_String'
         WHEN Research_And_Development_Desc_String LIKE '%tokyo%' THEN 'Research_And_Development_Desc_String'
         WHEN Distribution_String LIKE '%tokyo%' THEN 'Distribution_String'
         WHEN Sales_And_Marketing_Desc_String LIKE '%tokyo%' THEN 'Sales_And_Marketing_Desc_String'
      ELSE NULL END AS 'tokyo was here!'
   FROM YourTableName
       WHERE Goods_Services_String LIKE '%tokyo%' OR 
             Input_Materials_String LIKE '%tokyo%' OR 
             Property_Plant_Equipment_String LIKE '%tokyo%' OR 
             Production_Process_String LIKE '%tokyo%' OR 
             Capital_Expenditures_String LIKE '%tokyo%' OR 
             Employee_Locations_String LIKE '%tokyo%' OR 
             Contractors_Locations_String LIKE '%tokyo%' OR 
             Research_And_Development_Desc_String LIKE '%tokyo%' OR 
             Distribution_String LIKE '%tokyo%' OR 
             Sales_And_Marketing_Desc_String LIKE '%tokyo%';

huangapple
  • 本文由 发表于 2023年2月19日 01:16:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75495028.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定