我正在使用 postgres 表验证规则并尝试CHECK
为数组列设置约束。一个想法是只允许长度 > 0 的数组。
这是我想要实现它的方式:
create table words_table (
id serial primary key,
words varchar(20)[] CHECK (array_length(words, 1) > 0)
);
Run Code Online (Sandbox Code Playgroud)
但看起来它不起作用。o_o
insert into words_table (words) values ('{}');
//INSERT 0 1
Run Code Online (Sandbox Code Playgroud)
如何实现这样的约束?
我在 Postgres 中有以下表结构:
id | account_id | plan_id | active
----+------------+---------+--------
1 | 0cYd7Ak | 1 | f
2 | Uk02q1d | 1 | t
3 | eRlk810 | 2 | f
4 | Uk02q1d | 2 | t
5 | 0cYd7Ak | 1 | t
6 | yT3nv3p | 3 | t
Run Code Online (Sandbox Code Playgroud)
如何选择account_id
出现多次的所有行?对于上面的示例,它将返回第 1、2、4、5 行
我试过这个查询,但它没有返回我所期望的:
select * from table_name t1
where (select count(*) from table_name t2
where t1.account_id = t2.account_id) > 1
order by t1.account_id;
Run Code Online (Sandbox Code Playgroud)