我有两个字符串列a和b一个表foo.
select a, b from foo返回值a和b.但是,串联a和b不起作用.我试过了 :
select a || b from foo
Run Code Online (Sandbox Code Playgroud)
和
select a||', '||b from foo
Run Code Online (Sandbox Code Playgroud)
从评论更新:两列都是类型character(2).
在Microsoft SQL Server中,可以指定"重音不敏感"排序规则(对于数据库,表或列),这意味着可以进行类似的查询
SELECT * FROM users WHERE name LIKE 'João'
Run Code Online (Sandbox Code Playgroud)
找到一个带有Joao名字的行.
我知道可以使用unaccent_string contrib函数从PostgreSQL中删除字符串中的重音符号,但我想知道PostgreSQL是否支持这些"重音不敏感"排序规则,以便SELECT上述方法可行.
CREATE TABLE my_app.person
(
person_id smallserial NOT NULL,
first_name character varying(50),
last_name character varying(50),
full_name character varying(100) generated always as (concat(first_name, ' ', last_name)) STORED,
birth_date date,
created_timestamp timestamp default current_timestamp,
PRIMARY KEY (person_id)
);
Run Code Online (Sandbox Code Playgroud)
Error: generation expression is not immutable
The goal is to populate the first name and last into the full name column.
sql postgresql database-design string-concatenation calculated-columns
我已经定义了以下索引:
CREATE INDEX
users_search_idx
ON
auth_user
USING
gin(
username gin_trgm_ops,
first_name gin_trgm_ops,
last_name gin_trgm_ops
);
Run Code Online (Sandbox Code Playgroud)
我正在执行以下查询:
PREPARE user_search (TEXT, INT) AS
SELECT
username,
email,
first_name,
last_name,
( -- would probably do per-field weightings here
s_username + s_first_name + s_last_name
) rank
FROM
auth_user,
similarity(username, $1) s_username,
similarity(first_name, $1) s_first_name,
similarity(last_name, $1) s_last_name
WHERE
username % $1 OR
first_name % $1 OR
last_name % $1
ORDER BY
rank DESC
LIMIT $2;
Run Code Online (Sandbox Code Playgroud)
该auth_user表有620万行.
查询的速度似乎在很大程度上取决于查询可能返回的结果数similarity.
通过增加相似性阈值有set_limit帮助,但通过消除部分匹配来降低结果的有用性.
有些搜索在200ms内返回,其他搜索需要大约10秒. …
我们应该使用连接运算符||还是format()触发函数中的函数?
使用其中一种相对于另一种是否有任何优势,或者是否取决于个人喜好和可读性?
您是否会说对于简单的串联,使用运算符,但对于更复杂的串联,使用格式函数?
我在Postgres有一个表,它目前NOT NULL对它的email列有一个约束.该表还有一个phone可选的列.我想系统接受一些没有记录email,但只有当这些都phone为NOT NULL.换句话说,我需要一个NOT NULL数据库约束,使得CREATE或者UPDATE如果一方或双方的查询没有任何错误成功email或phone字段存在.
进一步扩展上述内容,是否可以在Postgres中指定一组列名,其中一个或多个应该是NOT NULL为了成功更新或创建记录?
这篇博文展示了如何immutable_concat在 Pg 中创建函数的示例:
CREATE OR REPLACE FUNCTION immutable_concat(VARIADIC "any")
RETURNS text AS 'text_concat'
LANGUAGE internal IMMUTABLE
Run Code Online (Sandbox Code Playgroud)
我想做同样的事情concat_ws并且相应的text_concat_ws确实存在,但是,以下只会使过程崩溃:
CREATE OR REPLACE FUNCTION immutable_concat_ws(VARIADIC "any")
RETURNS text AS 'text_concat_ws'
LANGUAGE internal IMMUTABLE
Run Code Online (Sandbox Code Playgroud)
更新: 的 siguatureimmutable_concat_ws应该是(glue, *parts),一个胶水(text 或 varchar)和一个或多个部分(text、varchar 或 null)。
我在这里缺少什么?
有人可以解释这三个查询之间的性能差异吗?
concat() 功能:
explain analyze
select * from person
where (concat(last_name, ' ', first_name, ' ', middle_name) like '%???%');
Seq Scan on person (cost=0.00..4.86 rows=1 width=15293) (actual time=0.032..0.140 rows=6 loops=1)
Filter: (pg_catalog.concat(last_name, ' ', first_name, ' ', middle_name) ~~ '%???%'::text)
Total runtime: 0.178 ms
Run Code Online (Sandbox Code Playgroud)
SQL标准串联||:
explain analyze
select * from person
where ((last_name || ' ' || first_name || ' ' || middle_name) like '%???%');
Seq Scan on person (cost=0.00..5.28 rows=1 width=15293) (actual time=0.023..0.080 rows=6 loops=1)
Filter: ((((((last_name)::text …Run Code Online (Sandbox Code Playgroud) sql postgresql concatenation pattern-matching postgresql-performance
我有以下查询:
select col1, col2
from ...
where...
Run Code Online (Sandbox Code Playgroud)
这使:
col1 col2
5
17
4 5
12
5
20
4 17
2 3
Run Code Online (Sandbox Code Playgroud)
我想将其转换为没有重复的一列,如下所示:
col3
5
17
4
12
20
2
3
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?我读了这个主题,将两列合并并添加到一个新列中,但这不是我所需要的...操作符||在这里没有帮助。
编辑: col3只是出现在col2和col1中的所有数字的列表。
postgresql ×9
sql ×6
coalesce ×1
concat ×1
concat-ws ×1
constraints ×1
database ×1
dynamic-sql ×1
function ×1
indexing ×1
localization ×1
pg-trgm ×1
similarity ×1
trigram ×1
types ×1