如何在PostgreSQL中转义下划线 [英] How to escape underscores in Postgresql
问题描述
在Postgresql中搜索下划线时,不能直接使用字符 _
。例如,如果您想在所有表格中搜索以 _by
结尾的任何列,例如更改日志或活动信息,例如 updated_by
, reviewed_by
等,以下查询几乎可以正常工作:
When searching for underscores in Postgresql, literal use of the character _
doesn't work. For example, if you wanted to search all your tables for any columns that ended in _by
, for something like change log or activity information, e.g. updated_by
, reviewed_by
, etc., the following query almost works:
SELECT table_name, column_name FROM information_schema.columns
WHERE column_name LIKE '%_by'
它基本上完全忽略下划线,并返回,就像您搜索了 Like'%by'
一样。在所有情况下这可能都不是问题,但有可能成为一个问题。如何搜索下划线?
It basically ignores the underscore completely and returns as if you'd searched for LIKE '%by'
. This may not be a problem in all cases, but it has the potential to be one. How to search for underscores?
推荐答案
您需要使用反斜杠来转义下划线。将示例查询更改为以下内容:
You need to use a backslash to escape the underscore. Change the example query to the following:
SELECT table_name, column_name FROM information_schema.columns
WHERE column_name LIKE '%\_by'
这篇关于如何在PostgreSQL中转义下划线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!