How to make a LIKE clause with a param in MYSQL? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to make a LIKE clause with a param in MYSQL?

I want to filter some content LIKE some user input stored into a param. I’m addition, I want that query to ignore both case and accent. Could give me any ideas?

6th Feb 2023, 6:01 PM
Tomás Atrat
1 Answer
+ 3
You can turn both the column content as well as the user input to the same case (like both upper) to make the query case-insensitive. To search for the input anywhere in the field text, enclose the content in % signs: WHERE UPPER(col) LIKE '%INPUT%' To ignore accents, check how to add COLLATE clause with appropriate character set. https://dev.mysql.com/doc/refman/8.0/en/charset-collate.html Another piece of advice: sanitize your input (remove special characters) to avoid the possibility of SQL injection attacks, and use prepared statement (or equivalent in your programming language) to assemble the SQL query, rather than just combining strings.
6th Feb 2023, 7:22 PM
Tibor Santa
Tibor Santa - avatar