How to use like function in a set of data in mysql? - 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

Related

Multiple search and find in sql

I'm new to SQL and I'm stuck on a multiple find in SQL.
Here I'm trying to search table1 in the three columns. But I'm stuch in trying to find two phrases. I'm trying with an OR statement.
If I remove OR LIKE '%paris%' it works but how do I find multiple words/phrases. And would this statement be case sensitive?
I'm also using MySQL to run the above.
SELECT * FROM `table1`
WHERE
CONCAT_WS('|',`target_1`,`target_2`,`target_3`)
LIKE '%london%' OR LIKE '%paris%'
In your code your second condition is sintactically wrong because is missing the a part for the match
so you should repeat the condition as
SELECT *
FROM `table1`
WHERE CONCAT_WS('|',`target_1`,`target_2`,`target_3`) LIKE '%london%'
OR CONCAT_WS('|',`target_1`,`target_2`,`target_3`) LIKE '%paris%'
One option is to use regular expression, then you can also have case insensitive matching (3rd parameter to REGEXP_LIKE)
SELECT *
FROM table1
WHERE REGEXP_LIKE(CONCAT_WS('|',`target_1`,`target_2`,`target_3`), 'london|paris', 'i');
You should repeat the left operand and use (maybe?) multiple conditions i/o concatenating:
SELECT * FROM `table1`
WHERE (`target_1` like '%london%' OR `target_1` like '%paris%')
AND (`target_2` like '%london%' OR `target_2` like '%paris%')
AND (`target_3` like '%london%' OR `target_3` like '%paris%')

Mysql - how to use like on multiple columns

I've searched in mysql query a word in multiple column in java program. The number of column is variable.
It is correct this query:
select * from customer with (city, name) like%'adelaide'%
You can use CONCAT() function:
select * from customer WHERE concat(city,name) like '%adelaide%'
You can add as many columns to the concat function as you like.
Also as you see I changed your like syntax, '%WORD%' and used a simple where clause.
It's better to use CONCAT_WS() function.
CONCAT() returns NULL if any argument is NULL.
CONCAT_WS() skip any NULL values after the separator argument.
multiple like statements can not be used with or directly. You have to use column name for each like statement.
Use multiple like as mentioned below.
Select *
from animals
where
(
[animalscolumn] like ('%dog%') or
[animalscolumn] like ('%cat%') or
[animalscolumn] like ('%gerbil%') or
[animalscolumn] like ('%hamster%') or
[animalscolumn] like ('%guinea pig%')
)
select * from customer with city like%'adelaide'% or name like%'adelaide'%

like clause doesn't work as it said in MySQL SQL

I have a table, such as
create table table1(
name varchar(32),
);
And there's some data in it. When I select like this:
select * from table1 where name like 'Jack2%';
there will be Jack2.
But if I select like this:
select * from table1 where name like 'Jack[0-9]%';
there will be nothing;
And I also tried regexp to subsitute like, but it also didn't work!
What's wrong?
You've confused two different pattern-matching mechanisms. SQL LIKE uses % to match anything and _ to match any single character; it does not have anything like [0-9] to match a digit. That looks like a character class from a regular expression.
Standard SQL has no support for regular expressions at all, but MySQL does - you just have to use RLIKE (or REGEXP, but that doesn't read as nicely IMO) instead of LIKE. But that means that you have to replace the % with the regular-expression equivalent .*, too.
SELECT * FROM table1 WHERE name RLIKE 'Jack[0-9].*';
Fiddle
MySQL REGEX
select * from Table1 where `name` REGEXP 'Jack[0-9]'
You can use RLIKE instead
SELECT * FROM table1 WHERE name RLIKE 'Jack[0-9].*';
And please note the the '%' operator won't work with RLIKE, you have to use a regular expression pattern like '.*' instead.

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.

MySQL Parameterized Query using Like

I want to run a query like this in MySQL:
select * from table where column1 like '%searchdata%'
But I want to use a parameter to pass in the search text. Is this possible? This doesn't seem to work:
select * from table where column1 like '%?Parameter%'
The % symbols need to be inside the parameter value, so it's something more like:
select * from table where column1 like ?;
And then you set the parameter to:
%searchText%