Problem when exiting character "%"

I have a table of images with a column of names. The value inside the name column can contain a percent sign, which itself is a wildcard symbol when using a similar operator. I currently have the following values ​​in the name column: "a%" and "blah% blah".

I am not getting any results as expected when doing:

select name from image where name like '\%'

      

When doing the following, I get 2 of the above entries:

select name from image where name like '%\%'

      

I understand why I am getting an entry named "%" as the name. However, I do not understand why I am getting blah blah blah. If "blah% blah" had a percentage character as the last character, that would make sense, but it doesn't.

Am I doing something wrong here or is this a MySQL bug?

+3


source to share


1 answer


As described in this error , you need to use a different mapping as a workaround:

SQL Fiddle

Using utf8_unicode_ci colating :

CREATE TABLE image
    (`id` int, `name` varchar(55) collate utf8_unicode_ci)
;

INSERT INTO image
    (`id`, `name`)
VALUES
    (1, 'a%'),
    (2, 'blah%blah')
;

      

Request 1 :

SELECT
  name
FROM
  image
WHERE name like '%\%'

      

Results :

|      name |
|-----------|
|        a% |
| blah%blah |

      



SQL Fiddle

Using the utf8_general_ci utility :

CREATE TABLE image
    (`id` int, `name` varchar(55) collate utf8_general_ci)
;

INSERT INTO image
    (`id`, `name`)
VALUES
    (1, 'a%'),
    (2, 'blah%blah')
;

      

Request 1 :

SELECT
  name
FROM
  image
WHERE name like '%\%'

      

Results :

| name |
|------|
|   a% |

      

+1


source







All Articles