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 query is as follows. mysql> select *from UserVariableInLike; The following is the output. mysql> set @searchName='John'; Query OK, 0 rows affected (0.00 sec) mysql> select id,Name,Age from UserVariableInLike where Name like CONCAT('%', @searchName, '%');
Does Ilike work in MySQL? It is in PostgreSQL the keyword ILIKE can be used instead of LIKE to make the match case-insensitive according to the active locale. This is not in the SQL standard but is a PostgreSQL extension. In MySQL you do not have ILIKE.
A parameterized query is a query in which placeholders are used for parameters and the parameter values are supplied at execution time. The most important reason to use parameterized queries is to avoid SQL injection attacks.
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%
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With