换行符会影响Postgres LIKE文本搜索吗?

时间:2019-07-05 01:47:13

标签: postgresql

所以我将以下内容存储在Postgres的文本列中...

This is a sentence.\nAnd here we start a new sentence.\nThen finally we have a third sentence.

那些\n会影响该列上的LIKE查询吗?

SELECT "tracks".* FROM "tracks" WHERE (info LIKE '%sentence%')

我问的原因是我希望换行以正确格式化文本的输出,但显然不希望它们搞乱搜索。

1 个答案:

答案 0 :(得分:1)

只要您将换行符存储为换行符,它就不会弄乱您的查询。

CREATE TABLE example(
   id serial PRIMARY KEY,
   text TEXT NOT NULL
);

INSERT INTO example (text) VALUES (E'This is a sentence.\nAnd here we start a new sentence.\nThen finally we have a third sentence.')

SELECT * FROM example WHERE text LIKE '%nAnd%'; -- 0 records returned
SELECT * FROM example WHERE text LIKE '%And%'; -- 1 record returned

将字符串插入表中时,请注意字符串前的E