How do I convert a column to ASCII on the fly without saving to check for matches with an external ASCII string?

前端 未结 4 1341
夕颜
夕颜 2021-01-19 09:25

I have a member search function where you can give parts of names and the return should be all members having at least one of username, firstname or lastname matching that i

4条回答
  •  鱼传尺愫
    2021-01-19 09:54

    First off, it should work this way:

    SELECT * FROM `test` WHERE `name` COLLATE utf8_general_ci LIKE '%renee%';
    

    Where the test table is:

    +-----+--------+
    | id  | name   |
    +-----+--------+
    |  1  | Renée  |
    |  2  | Renêe  |
    |  3  | Renee  |
    +-----+--------+
    

    What is your MySQL version, and how do you try to match things?


    One of the other possible solutions is transliteration.

    Related: PHP Transliteration

    Transliterating the input should not be a problem, but transliterating the values from the permanent storage (e.g. db) real-time during the search may not be feasible. So you can add three more fields like: username_slug, firstname_slug and lastname_slug. When inserting/modifying a record, set the slug values appropriately. And when searching, search the transliterated input against that slug fields.

    +------+----------+---------------+----------+---------------+ ...
    | id   | username | username_slug | lastname | lastname_slug | ...
    +------+----------+---------------+----------+---------------+ ...
    |    1 | Renée    |    renee      | La Niña  | la-nina       | ...
    |    2 | Renêe    |    renee      | ...      | ...           | ...
    |    3 | Renee    |    renee      | ...      | ...           | ...
    +------+----------+---------------+----------+---------------+ ...
    

    A search for "renee" or "renèe" would match all of the records.

    As a side effect, you may be able to use that fields for generating SEF (search engine friendly) links, hence they are named ,..._slug, e.g. example.com/users/renee. Of course, in that case you should check for the uniqueness of the slug field.

提交回复
热议问题