Choosing sql query with value symbol% โ€‹โ€‹as value

Hello, I have a table in the database:

โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•— โ•‘ v3_url_alias โ•‘ โ• โ•โ•โ•โ•โ•ฆโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ฆโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ฃ โ•‘ id โ•‘ query โ•‘ keyword โ•‘ โ• โ•โ•โ•โ•โ•ฌโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ฌโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ฃ โ•‘ 1 โ•‘ product_id=20 โ•‘ 540-65R38-K_028 โ•‘ โ•‘ 2 โ•‘ product_id=21 โ•‘ 18.00R33-EM_DT-150% โ•‘ โ•šโ•โ•โ•โ•โ•ฉโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ฉโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ• 

I use this table to make my URLs friendly, but many people know that the% sign is not a valid URL character, so my browser receives a bad request. I have many records in this table (1700 or so) and need a query to capture all those that have% in the keyword.

So, I tried doing this in MySQL and came up with this query:

 SELECT * FROM v3_url_alias WHERE keyword LIKE '%%%'; 

This returned all my keywords since% is used as a wildcard.

My question is, how to get every keyword containing the% character with SQL?

+6
source share
3 answers

Use an escape character!

MySQL has a backslash ( \ ) as the default escape character:

 SELECT * FROM v3_url_alias WHERE keyword LIKE '%\%%'; 

ANSI SQL - use the ESCAPE clause to specify an escape character, for example:

 SELECT * FROM v3_url_alias WHERE keyword LIKE '%#%%' escape '#'; 

(This also works with MySQL, at least as long as \ not specified.)

+5
source

Use square brackets.

 SELECT * FROM v3_url_alias WHERE keyword LIKE '%[%]%' 
+4
source

Using MySQL:

Use backslash to escape the character (\).

 SELECT * FROM v3_url_alias WHERE keyword LIKE '%\%%'; 
0
source

All Articles