首页 > 解决方案 > 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 格式

标签: mysqlmongodbstudio3t

解决方案


您可以在此处阅读 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 字符串文字中的字符没有什么特别之处。


推荐阅读