Using OR in LIKE Query in MySQL to compare multiple fields - mysql

I always thought that you could use OR in a LIKE statment to query things in MySQL. So, if I wanted to compare multiple fields in a row to 1 keyword or term:
SELECT * FROM MyTable WHERE Column1 OR Column2 LIKE '%keyword%';
and if I had an array of words to compare:
SELECT * FROM MyTable WHERE Column1 OR Column2 LIKE '%keyword1%'
AND Column1 OR Column2 LIKE '%keyword2%';
I don't believe that syntax is correct, however. Is there an efficient method of writing this aside from something like:
SELECT * FROM MyTable WHERE Column1 LIKE '%keyword1%' OR Column2 LIKE
'%keyword1%' AND Column1 LIKE '%keyword2%' OR Column2 LIKE '%keyword2%';
Am I going about this correctly?

Use this::
SELECT * FROM MyTable WHERE (Column1 LIKE '%keyword1%' OR Column2 LIKE
'%keyword1%') AND (Column1 LIKE '%keyword2%' OR Column2 LIKE '%keyword2%');

The closest to the syntax you are desiring is:
SELECT * FROM MyTable
WHERE (CONCAT(Column1, Column2) LIKE '%keyword1%')
AND (CONCAT(Column1, Column2) LIKE '%keyword2%')
Note: that the "%" at the start of your search string precludes the use of indexes. If there are any large number of records to search, it would be best to rethink the implementation.
If you cannot guarantee that each column is not NULL, then use CONCAT_WS instead:
SELECT * FROM MyTable
WHERE (CONCAT_WS("-", Column1, Column2) LIKE '%keyword1%')
AND (CONCAT_WS("-", Column1, Column2) LIKE '%keyword2%')
This CONCAT_WS solution also has the possible benefit of assuring that matches of your "keyword" where in only in Column1 OR Column2, if you select a separator character that is never present in your keywords.

You can compare a field to multiple strings at once in MySQL with REGEXP_LIKE()
SELECT *
FROM MyTable
WHERE REGEXP_LIKE(Column1, 'keyword1|keyword2') OR REGEXP_LIKE(Column2, 'keyword1|keyword2')

Related

Look for one value in multiple Columns SQL

I would like to search one value in multiple columns of my table.
Im making
SELECT* FROM myTableName WHERE'value' IN(column1, column2, column3);
This is working for me, but i wan't to make it with LIKE. Something like this:
SELECT* FROM myTableName LIKE '%value%' IN(column1, column2, column3);
Because i'm making a browser, so i need to use the LIKE.
I'm getting error and i donĀ“t know how can i make it well.
Anyone knows something?
With PostgreSQL there is the ANY or ALL form:
WHERE col LIKE ANY( subselect )
or
WHERE col LIKE ALL( subselect )
where the subselect returns exactly one column of data.
You can try to use OR with LIKE instead of IN with LIKE
SELECT *
FROM myTableName
WHERE
column1 LIKE '%value%'
OR
column2 LIKE '%value%'
OR
column3 LIKE '%value%'
NOTE:
I will store column1,column2,column3 in one column if you need to search it because one column with only one LIKE will be higher performance than you used multiple columns.
select #q := concat('select * from Table1 ',
'where concat(', group_concat(column_name), ', "") like ?'
)
from information_schema.columns c
where table_name = 'Table1';
set #p = '%augusto%';
prepare st from #q;
execute st using #p;
deallocate prepare st;
Ref:Mysql query search a string in all columns of a table
You can use three like patterns with or:
WHERE column1 LIKE '%value%' OR
column2 LIKE '%value%' OR
column3 LIKE '%value%'
However, that is cumbersome because you have to repeat the pattern multiple times. There is a work-around that works in more recent versions of MySQL:
where exists (select 1
from (select t.column1 as val union all
select t.column2 as val union all
select t.column3 as val
) x
where x.val like '%value%'
)
This does not work in older versions of MySQL because the double nested correlation is not supported.
(Here is an example.)

SQL - Trying to tell which part of a query was matched with an 'OR' statement

I have a query where I want to check if multiple columns match what I'm looking for, like so:
$params = [$param1,$param2];
$sql = "SELECT * FROM table WHERE column1 LIKE ? OR column2 LIKE ?";
$stmt = DB::run($sql,$params);
In this example I'm ust using two columns but on my site I've got 8 and this could increase.
But I want to echo out the data from the column that matches and not the other columns.
So logically speaking I want something like: if column1 matches my params echo column1 else if column2 matches my params echo column2.
Will I need to do a query for each of these individually or is there a way to tell when you have an 'OR' statement like in the query above which part of the query is being matched.
use union , According to your demand your query should be like below
SELECT column1 FROM table WHERE column1 LIKE ?
union
SELECT column2 FROM table WHERE column2 LIKE ?
If all you want is a single column, I'd go with the UNION approach described in another answer here.
If you want something to embed in a wider query, you may be well served by a CASE expression.
SELECT
*,
CASE WHEN column1 LIKE ? THEN column1
WHEN column2 LIKE ? THEN column2
ELSE NULL END AS newColumn
FROM
table
WHERE
column1 LIKE ?
OR column2 LIKE ?
(Both suggestions assume that column1 and column2 are the same datatype.)
If you want to allow multiple rows to match, then you can use union all as in:
select 'column1' as which, column1
from t
where column1 like ?
union all
select 'column2' as which, column2
from t
where column2 like ?;
Or, if performance is not a big deal and you only want to list the condition once:
select which, val
from (select 'column1' as which, column1 as val
from t
union all
select 'column2' as which, column2
from t
) t
where val like ?;
This lists each match on a separate row. I would be inclined, though, to list them in a single row:
select concat_ws(',',
case when column1 like ? then 'column1' end,
case when column2 like ? then 'column1' end
)
from t
where column1 like ? or column2 like ?;
In this case, duplicating the conditions can be cumbersome, so you can also do:
select concat_ws(',',
case when column1 like ? then 'column1' end,
case when column2 like ? then 'column1' end
) as matches
from t
where matches > '';

SQL connect two columns

I've got a table with two columns like
column1 column2
01234 56789
33333 883737
What I need is something like this, but I've no idea how to sql this:
SELECT * FROM table1 WHERE (value1|+|value2)='0123456789'
Any ideas?
Use the CONCAT function, as below:
SELECT *
FROM table1
WHERE CONCAT(column1, column2) = '0123456789';
Reference:
CONCAT function on MySQL Reference Manual
Something like
SELECT * FROM table1 WHERE CONCAT(value1,value2)='0123456789'
note, you lose the indexing advantages by using this function.
I assumed value1 and value2 are tge column names vs column1 column2.

How to use like function in a set of data in mysql?

is there a way i can use like function in a set of data?
i tried
select * from table1 where column1 like ('%1%','%2%','%3%','%4%');
but it didn't work.
my scenario is having 2 select statements such as select * from class1 where firstname in (select name from class2 where firstname = 'greg');
but instead of having class1.firstname = class2.firstname i wanted it to be class1.firstname like concat('%',class2.firstname,'%');
You could "OR" them:
... WHERE (column1 like '%1%') OR (column1 like '%2%') OR ...
like takes one argument
select *
from table1
where column1 like ('%1%');
if you want to check for many then use OR or UNION operators
select *
from table1
where column1 like ('%1%')
or column1 like ('%2%');
Normally you would use the IN function when you have a list of strings to compare to. But here you need a combination of LIKE and IN, and as far as I know there is no function in SQL that does this, so you'll need to use the OR operator.
You can use the execute to build the dynamic sql, but you must beware of safety and performance issues that may arise.
Another options is using regex

MySQL IN() for two value/array?

I'm having trouble finding a better way to search MySQL for a pair of values in a table. I have the value pairs in an array, and would like to duplicate the IN() function, but for more than 1 value.
For example purposed; I have the following 3 pairs:
foo,1
boo,2
goo,3
The current solution puts me at:
SELECT * FROM [table] WHERE
(column1 = 'foo' AND column2 = 1) OR
(column1 = 'boo' AND column2 = 2) OR
(column1 = 'goo' AND column2 = 3);
I'd like to think there's a more "sexy" solution seeing that I could have as many as a hundred pairs and having that may ORs kind of makes me nauseous. Thanks!!!
SELECT *
FROM foo
WHERE (column1, column2) IN (('foo', 1), ('bar', 2))
This syntax may be confusing, and it may be more readable to replace it with:
SELECT *
FROM foo
WHERE ROW(column1, column2) IN (ROW('foo', 1), ROW('bar', 2))
I'm used to the former one, though :)
If you can get your values into a temp table (you only need the two columns) easily and quickly, you can just INNER JOIN your way there. If not, you'll have to use #Quassnoi version.
Great answers from #Quassnoi and #KM !!!
Also, you can get pairs duplicates and select them for post-processing:
SELECT *
FROM `foo`
WHERE (`id_obj` , `Foo_obj`)
IN (
SELECT `id_obj` , `Foo_obj`
FROM `foo`
GROUP BY `id_obj` , `Foo_obj`
HAVING count(*) > 1
)