SELECT *
FROM TABLE
WHERE COLUMN_NAME LIKE = '%ABC%'
This query will search for a string in any column and return the result. But I want to get all rows which contain that string in any of the columns.
Is there any way to get the output after filtering in all the columns?
NOTE: I have a table which has 50 columns, so using a for loop and hitting the database 50 times (one for each column) is not time efficient.
1条答案
按热度按时间tjvv9vkg1#
We can concat the strings of those columns and apply the condition on this entire string:
Of course, there are some possible edge cases that this would fetch rows where for example column1 has a string 'AB' and column2 a string 'CD' and such rows should not be selected. If this is the case, we will need to use
OR
instead.Or we can follow the first option and add some separator between the columns:
In my opinion, all these options are unpleasant. That's because pure SQL is not good in such things.
I recommend to do this in your application rather than as pure SQL query. Or you could at least find a complete string to search in the columns to avoid the
LIKE
. Then you could do this: