我需要获取 eventname
字段包含此文本(包括单引号)的所有行
's Birthday
我试过了,但它给出了错误:
select * from fab_scheduler where eventname like '%\'s Birthday%'
我如何构建这样的查询?
谢谢
最佳答案
如果您打算在 SQL 中包含 's,则需要 escape它:
There are several ways to include quote characters within a string:
- A “'” inside a string quoted with “'” may be written as “''”.
- A “"” inside a string quoted with “"” may be written as “""”
- Precede the quote character by an escape character (“\”)
- A “'” inside a string quoted with “"” needs no special treatment and need not be doubled or escaped. In the same way, “"” inside a string quoted with “'” needs no special treatment.
此外,如果您在程序中构建 SQL,您应该强烈考虑使用 mysql_real_escape_string
转义进入 SQL 查询的变量。或其在您的编程语言中的等价物。如果不这样做,您的应用程序将容易受到控制所述变量数据源的任何人(可能是您的用户;请记住,“您的用户”可能意味着“整个互联网”,具体取决于具体情况)的 SQL 注入(inject)攻击。
编辑:引用上面的dev.mysql.com说明。
关于php - 关于查找文本的 mysQL 查询帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2811593/