youv youv - 1 year ago 79
MySQL Question

how the query works, select any name ends with %

I need to fetch all names ends with a % at the end.

My query is working but don't know how:-

1. select name from table where name like '%\%%'

2. select name from table where name like '%%\%'

May i know how this query works and difference? the backslash position not matters, how?

please help:-

Answer Source

What you are confused about is the % as a wildcard. It can match any number of characters -- or none.

So, you want:

where name like '%\%'

The two wildcards together (%%) are the same as one. The first version will match something with % in the middle of the string.

Or, alternatively:

where right(name, 1) = '%'  -- less confusing
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download