I have a table with two string columns: Url and ModelId. I need to return records for which Url contains ModelId, something like this:
SELECT Id, Url, ModelId WHERE Url like "%ModelId%"
The SQL LIKE OperatorThe LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator: The percent sign (%) represents zero, one, or multiple characters. The underscore sign (_) represents one, single character.
In MySQL you do not have ILIKE. Checkout MySQL docs on string comparison functions.
The SQL EXISTS Operator The EXISTS operator is used to test for the existence of any record in a subquery. The EXISTS operator returns TRUE if the subquery returns one or more records.
SELECT Id, Url, ModelId WHERE Url LIKE CONCAT('%', ModelId, '%')
You can not just concat the strings, you must also escape the field from % and _:
SELECT Id, Url, ModelId WHERE Url LIKE CONCAT('%', REPLACE(REPLACE(ModelId,'%','\%'),'_','\_'), '%'), '%')
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