选择复合类型数组的条件(postgresql)

发布于 2024-08-27 12:25:56 字数 242 浏览 4 评论 0原文

例如,我有类型:

CREATE TYPE record AS ( name text, description text, tags text[])

和表:

CREATE TABLE items ( id serial, records record[] )

如何选择带有标签“test”的记录的所有项目(不使用 PL/pgSQL)?

For example I have type:

CREATE TYPE record AS ( name text, description text, tags text[])

And table:

CREATE TABLE items ( id serial, records record[] )

How can I select all items with records with tags 'test' (without using PL/pgSQL)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

因为看清所以看轻 2024-09-03 12:25:56

为什么大家都想用数组、hstore等搬起石头砸自己的脚?将数据规范化为标准 SQL 表。并在编程时使用数组、hstore 等高级功能。但这里有一个要点……

Postgres 不会喜欢你使用保留字作为类型。

CREATE TYPE rec AS (name text, description text, tags text[]);
CREATE TABLE items (id int, wreck rec);
INSERT INTO items(1, row('foo','foo description', '{test,testing,tested}')::rec);

SELECT * 
FROM items
WHERE 'test' = ANY ((wreck).tags)

在一个数组中、另一个数组中的复合体中搜索文本……这真是令人难以置信。即使你确实弄清楚了,任何追随你试图维护你的代码的人都会摸不着头脑。

Why does everyone want to shoot themselves in the foot with arrays, hstores, etc? Normalize your data into standard SQL tables. And use advanced features like arrays, hstores when you are programming. But here's a bullet...

Postgres isn't going to like your use of a reserved word as a type.

CREATE TYPE rec AS (name text, description text, tags text[]);
CREATE TABLE items (id int, wreck rec);
INSERT INTO items(1, row('foo','foo description', '{test,testing,tested}')::rec);

SELECT * 
FROM items
WHERE 'test' = ANY ((wreck).tags)

And searching for text in an array, in a composite in another array... well that just boggles the mind. And even if you did figure it out, anyone who came after you trying to maintain your code would be left scratching their heads.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文