MySQLi WHERE LIKE multiple criteria

前端 未结 4 1959
暖寄归人
暖寄归人 2021-01-13 10:01

I have the following example table and attributes:

---------------------------
|  Name  |       Town     |
---------------------------
| Name 1 |      POOLE          


        
4条回答
  •  臣服心动
    2021-01-13 10:07

    This is an iteration of an often-asked class of questions: How do I select on a single datum, if I have more than one in a field?

    The answer, as always, is: You don't.

    There are many reasons for that, but one of the most important is performance: Basically a LIKE '%...' can't use an index. That might be ok with a handful of test rows, but it quickly becomes a problem when scaling.

    The only reliable ways are to

    • either normalize your data
    • or use a fulltext index

    In your case I'd strongly vote for normalization: Create a towns table, then link it to the players via a join table. You can now search for any town with full index use, finding the players through the join.

提交回复
热议问题