【问题标题】:How to select records that have apostrophe containers using the same words without apostrophe words?如何选择具有撇号容器的记录使用相同的单词而没有撇号?
【发布时间】:2021-12-13 15:23:37
【问题描述】:

如何选择具有撇号容器的记录?使用相同的词没有撇号?

+----+------------------------------------------+-------+
| id | name                                     | price |
+----+------------------------------------------+-------+
|  1 | World's Best Inspirational Books         | 199   |
|  2 | Ben Martin Men's Slim                    | 98    |
|  3 | Harry Potter and the Philosopher's Stone | 79    |
|  4 | Today's life                             | 690   |
|  5 | Life's Amazing Secrets                   | 99    |
+----+------------------------------------------+-------+
$search = "worlds";

SELECT * FROM product WHERE name LIKE "%$search%";

我想从$search = "world's";得到这个结果

+----+------------------------------------+-------+
| id | name                               | price |
+----+------------------------------------+-------+
|  1 | World's Best Inspirational Books   | 199   |
+----+------------------------------------+-------+

但我知道了,Empty set (0.001 sec)

如何使用 `worlds 搜索 world's(撇号) 单词(不包括撇号)?

【问题讨论】:

  • where replace(name, '''', '') like '%worlds%'
  • 这里无法复制dbfiddle.uk/…

标签: mysql sql database


【解决方案1】:

您可以通过以下方式转义撇号:

World''s

在程序中,一种“肮脏”的修复方法是在调用查询之前向参数内的所有实例添加一个额外的撇号 (')。

【讨论】:

  • OP 使用双引号字符串
  • @SalmanA 在这种情况下,OP 可以将它们转换为撇号。
【解决方案2】:

我不知道这在mysql中是否有效,但是在sql server中你可以这样做

declare @test table (name varchar(50))
insert into @test values ('World''s Best Inspirational Books'), 
                         ('Ben Martin Men''s Slim'),
                         ('Harry Potter and the Philosopher''s Stone'),
                         ('Today''s life'),
                         ('Life''s Amazing Secrets')

select * 
from   @test t
where  t.name like '%world_s%'

它会返回第一行。
因此,如果您不想在搜索中添加额外的',也许您可​​以将所有' 替换为_

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-20
    相关资源
    最近更新 更多