Get the column names matching a multiple column search

笑着哭i 提交于 2021-01-27 18:15:41

问题


For an application, I need to show search results based upon multiple columns. Searching in multiple columns is not that hard, but I also want to know which column(s) the search query matches the text.

Imagine following 'Blabla' table:

---------------------------------------------------------------------------------------
| column1                |  column2                  | column 3                       |
---------------------------------------------------------------------------------------
| Animals are beautiful  | Some text regarding music | Vague statement about politics |
---------------------------------------------------------------------------------------
| Music is not his thing | Green is a color          | Random gibberish about music   |
--------------------------------------------------------------------------------------- 

Searching for the word 'music' would be as simple as:

SELECT * FROM Blabla WHERE column1 LIKE '%music%' OR column2 LIKE '%music%' OR column2 LIKE '%music%';

But is there a way in SQL to show that matching columns would be column2 for row 1 and column1 + column3 for row 2?


回答1:


You could use a CASE statement:

SELECT CASE WHEN column1 LIKE '%music%' THEN 'Column1' ELSE '' END AS col1match,
       CASE WHEN column2 LIKE '%music%' THEN 'Column2' ELSE '' END AS col2match,
       CASE WHEN column3 LIKE '%music%' THEN 'Column3' ELSE '' END AS col3match
FROM Blabla



回答2:


That would be

select
  column1,
  column2, 
  column3,
  case
    when column1 like '%music%' then 'column1'
    when column2 like '%music%' then 'column2'
    when column3 like '%music%' then 'column3'
    else ''
  end as [column]
from Blabla where column1 like '%music%' or column2 like '%music%' or column3 like '%music%'

Not sure about how performant this actually is.




回答3:


Yes, you can use CASE EXPRESSION for indication columns :

SELECT s.* FROM (
    SELECT t.*,
           CASE WHEN t.column1 LIKE '%music%' THEN 1 ELSE 0 as col_1_ind,
           CASE WHEN t.column2 LIKE '%music%' THEN 1 ELSE 0 as col_2_ind,
           CASE WHEN t.column3 LIKE '%music%' THEN 1 ELSE 0 as col_3_ind
    FROM Blabla t) s
WHERE 1 IN(s.col_1_ind,s.col_2_ind,s.col_3_ind)



回答4:


try this,

declare @t table(column1 varchar(100), column2 varchar(100),column3 varchar(100))
insert into @t values
('Animals are beautiful','Some text regarding music','Vague statement about politics')

,('Music is not his thing','Green is a color','Random gibberish about music')

;With CTE as
(
 select column1,column2,column3,ROW_NUMBER()over(order by (select null))rn from @t
)
select case when column1 LIKE '%music%' then 'column1 in row'+ cast(rn as varchar) end
,case when column2 LIKE '%music%' then 'column2 in row'+ cast(rn as varchar) end
,case when column3 LIKE '%music%' then 'column3 in row'+ cast(rn as varchar)
end
from cte  


来源:https://stackoverflow.com/questions/40461595/get-the-column-names-matching-a-multiple-column-search

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