MySQL Parameterized Query using Like - mysql

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%

Related

mysql matching encrypted values

When I run a query like this:
SELECT * FROM table WHERE columnName = AES_ENCRYPT('value','SecretKey')
I'm returned an empty set, even though there are rows in the db that match the search query.
What would the correct syntax for something like this look like?
SELECT * FROM table WHERE columnName = AES_ENCRYPT(columnName,'SecretKey')='value'

How to query MySQL if data in one field look like 32;13;3;33 and the condition is where 3

I have a table in which one column is filled with data like 32;3;13;33;43
so
SELECT * FROM table;
gives something like
name ids
vegetables 13;3;63
fruits 37;73;333
When I'm querying MySQL like
SELECT * FROM table WHERE ids LIKE '%3%'
it gives me both records but obviously I want only this containing 3.
How to query MySQL correctly?
Try to use:
SELECT * FROM table WHERE CONCAT(';',ids,';') LIKE '%;3;%'
You will need to cover the case where it's the first in the list and the last.
SELECT * FROM table WHERE ids LIKE '%;3;%' OR LIKE '%;3' OR LIKE '3;%'
You can use FIND_IN_SET, if you replace the ; with a , before checking the value:0
SELECT *
FROM table
WHERE FIND_IN_SET('3', REPLACE(ids, ';', ','))

mysql like query with special character '

I wrote query for filter data using name and wrote following query
SELECT * FROM (`abc`) WHERE (name LIKE "%test\'!&##$\%-(3)\_er%")
It should return records which has name start with text "test"
but it will not instead of if I modify query like
SELECT * FROM (`abc`) WHERE (name LIKE "%test%\'!&##$\%-(3)\_er%")
then it will give result. Why it is not give result with first query?
Is there any other way to do this?
The % is the wildcard in the query.
So %test means everything that ends with test.
and test% means everything that begins with test.
and %test% means everything with test in it.
Simpy change your query to
SELECT * FROM (abc) WHERE (name LIKE "test%")
If you want records that start with test, simply use
SELECT * FROM (`abc`) WHERE (name LIKE "test%")

"Merge" IN and LIKE operator in Mysql

I've a problem and I'm guessing if there's a better way to achieve my goal. I've this query in Mysql to retrieve some rows:
SELECT *
FROM table
WHERE field IN ('V','S','G','B')
What I would like to do is to run a query that retrieve the rows where the field has value LIKE those in the IN list. I know that the trivial way is to use this query:
SELECT *
FROM table
WHERE field LIKE '%V%' OR field LIKE '%S%' OR
field LIKE '%G%' OR field LIKE '%B%'
What I want to know is there's an operator that do this or at least, if that operator does not exist, a better way to write the query.
Thanks to everyone that will help me.
Put the values in a table (Params) then use
SELECT *
FROM table
WHERE EXISTS (
SELECT *
FROM Params
WHERE table.field LIKE '%' + Params.col + '%'
);
Consider also putting the wildcard characters into the values in the Params table.

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