mysql - SQL查询IN子句:传递列表时如何转义特殊字符
问题描述
我在查询中传递了一个期刊列表,其中包含期刊名称中的特殊字符,如 &,-'()。这是我的查询的样子:
select alt_id, citation.doi, citation.journal, citation.volume,
citation.issue, citation.pubdate, citation.title
from ALT_2018
where citation.journal in ('Mechanics & Astronomy', 'Women's Health', 'Current
Directions in Psychological Science (Sage Publications Inc.)', 'Cement &
Concrete Composites', 'Journal of Heart & Lung Transplantation', 'Infection control
and hospital epidemiology (Online)', 'Journal of Research on Adolescence
(Blackwell Publishing Limited)', 'Ophthalmic & Physiological Optics', 'Brain
Structure & Function', 'New Solutions: A Journal of Environmental &
Occupational Health Policy', 'European Physical Journal B -- Condensed
Matter')
我尝试使用反斜杠来转义特殊字符,但它不起作用,我也无法手动执行,因为该列表包含 2000 多种期刊。请帮帮我。
PS 我在 Studio3T 中使用 SQL 查询扩展,因为数据保存在 MongoDB 中并且是 json 格式
解决方案
您可以在此处阅读 MySQL 字符串文字中的特殊字符:https ://dev.mysql.com/doc/refman/8.0/en/string-literals.html
如果您需要使用撇号字符,您可以使用以下方法之一:
'Women\'s Health'
'Women''s Health'
"Women's Health" -- only if sql_mode is not ANSI or ANSI_QUOTES
这些是选项。我不知道您所说的“它不起作用”是什么意思,所以我无法建议如何解决它。我不使用 Studio3T,所以我猜不出是什么阻止您使用这些解决方案之一。
&
MySQL 字符串文字中的字符没有什么特别之处。
推荐阅读
- excel - 仅从工作表中复制最后一行并将其粘贴到另一张工作表的最后一行
- javascript - 在 for 循环中计算的 Javascript
- webpack - 使用 next.js 从 node_modules 导入时如何修复较少的 syntaxError
- azure-cosmosdb - Cosmos DB - 使用自定义分区键批量导入(单个分区集合)
- excel - 使用 Outlook 将“行数据”和“范围/选择”数据合并为 VBA 中的邮件正文
- python - 如何在python中获得同义词/复数词的基础?
- angular - Angular 模型中的属性装饰器
- reactjs - React Javascript 中有没有办法传递道具并在外部导入中使用它?
- apache-spark - 写入文件时出现 Databricks 校验和错误
- java - MalformedJsonException:使用 JsonReader.setLenient(true) 接受格式错误的 JSON - Android Studio