MySQL selecting string with special characters

后端 未结 3 1700
我寻月下人不归
我寻月下人不归 2021-01-21 15:33

I\'m having a problem selecting strings from database. The problem is if you have McDonald\'s in row and if you are searching with a string mcdonalds i

3条回答
  •  长发绾君心
    2021-01-21 16:25

    If your search requirements are to ignore certain characters, you can remove them during a search by replaceing them with a blank.

    This answer solves your problem:

    SELECT *
    FROM restaurants
    WHERE replace(name, '''', '') like '%mcdonalds%'; -- This will match "McDonald's"
    

    FYI, a single quote literal (') is written as a doubled single quote (''), so to specify a single quote as a parameter to replace you need four quotes in a row ('''') - two at each end and the doubled quote in the middle for the actual quote.

提交回复
热议问题