我有一个表存储两个外键,实现:m 关系。
其中一个指向一个人(主题
),另一个指向一个特定项目。
现在,一个人可能拥有的物品数量在另一个表中指定,我需要一个查询,该查询将返回与一个人可能拥有的物品数量相同的行数。
其余记录可能会填充 NULL 值或其他值。
事实证明,从应用程序方面解决这个问题是很痛苦的,所以我决定尝试一种不同的方法。
编辑:
示例
CREATE TABLE subject_items
(
sub_item integer NOT NULL,
sal_subject integer NOT NULL,
CONSTRAINT pkey PRIMARY KEY (sub_item, sal_subject),
CONSTRAINT fk1 FOREIGN KEY (sal_subject)
REFERENCES subject (sub_id) MATCH SIMPLE
ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT fk2 FOREIGN KEY (sub_item)
REFERENCES item (item_id) MATCH SIMPLE
ON UPDATE CASCADE ON DELETE CASCADE
)
我需要一个查询/函数来返回所有主题项目(主题可能有 5 个项目)
但只有 3 个项目分配给该主题。
返回有点像:
sub_item | sal_subject
2 | 1
3 | 1
4 | 1
NULL | 1
NULL | 1
我正在使用 postgresql-8.3
I have a table that stores two foreign keys, implementing a n:m relationship.
One of them points to a person (subject
), the other one to a specific item.
Now, the amount of items a person may have is specified in a different table and I need a query which would return the same number of rows as the number of items a person may have.
The rest of the records may be filled with NULL
values or whatever else.
It has proven to be a pain to solve this problem from the application side, so I've decided to try a different approach.
Edit:
Example
CREATE TABLE subject_items
(
sub_item integer NOT NULL,
sal_subject integer NOT NULL,
CONSTRAINT pkey PRIMARY KEY (sub_item, sal_subject),
CONSTRAINT fk1 FOREIGN KEY (sal_subject)
REFERENCES subject (sub_id) MATCH SIMPLE
ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT fk2 FOREIGN KEY (sub_item)
REFERENCES item (item_id) MATCH SIMPLE
ON UPDATE CASCADE ON DELETE CASCADE
)
I need a query/function which would return all subject items (subject may have 5 items)
but there are only 3 items assigned to the subject.
Return would be somewhat like:
sub_item | sal_subject
2 | 1
3 | 1
4 | 1
NULL | 1
NULL | 1
I am using postgresql-8.3
发布评论
评论(3)
考虑这个 plpgsql 函数的很大程度上简化版本。应该在 PostgreSQL 8.3 中工作:
手册中有关 plpgsql 的详细信息(链接到版本 8.3)。
Consider this largely simplified version of your plpgsql function. Should work in PostgreSQL 8.3:
Details about plpgsql in the manual (link to version 8.3).
可以像这样工作(纯 SQL 解决方案):
请参阅
generate_series()
。关于窗口函数的手册。
LEFT JOIN
将现有项目与每个主题的理论项目相连接。缺失的项目用 NULL 填充。除了您在问题中披露的表之外,我假设有一列保存
subject
表中最大项目数:查询 PostgreSQL 8.3,替换缺失的窗口函数
row_number()
:有关在此 row_number() 的更多信息href="http://explainextended.com/2009/05/11/postgresql-emulated-row_number/" rel="nofollow">Quassnoi 的文章。
Could work like this (pure SQL solution):
See the manual for
generate_series()
.Manual about window functions.
LEFT JOIN
the existing items to the theoretical items per subject. Missing items are filled in with NULL.In addition to the table you disclosed in the question, I assume a column that holds the maximum number of items in the
subject
table:Query for PostgreSQL 8.3, substituting for the missing window function
row_number()
:More about substituting
row_number()
in this article by Quassnoi.我能够提出这个简单的解决方案:
首先返回我可能选择的所有值,然后在我们有正确的数量时循环返回空值。如果有人偶然发现同样的问题,请将其发布在这里。
仍在寻找更简单/更快的解决方案(如果存在)。
I was able to come up to this simplistic solution:
First returning all the values i may select then looping returning null values while we have the right amount. Posting it here if someone would stumble on the same problem.
Still looking for easier/faster solutions if they exist.