简体   繁体   English

为同一列返回具有不同特定数据的行或使用 POSTGRESQL 返回 0 行

[英]Return rows with different specific data for the same column or return 0 rows using POSTGRESQL

I am in college learning SQL vis Postgresql.我在大学学习 SQL 和 Postgresql。 This is my first post here so if I'm not posting correctly, let me know.这是我在这里的第一篇文章,所以如果我没有正确发布,请告诉我。 I have a question that has me stumped.我有一个问题让我很困惑。

A bank setup with multiple users having multiple accounts at multiple branches in multiple cities.多个用户在多个城市的多个分行拥有多个帐户的银行设置。 The question wants to find any users that have at least 1 account at every branch in a specific city.该问题希望找到在特定城市的每个分支机构至少拥有 1 个帐户的任何用户。

There are 3 branches in Brooklyn.布鲁克林有 3 家分店。 I need to find all user ID's that have at least one account in each branch.我需要找到在每个分支中至少有一个帐户的所有用户 ID。

If they do not have at least 1 account in each branch, it should not return anything for that user.如果他们在每个分支中没有至少 1 个帐户,则不应为该用户返回任何内容。

I don't know how to return multiple rows with different results from the same column and REQUIRE the specific branch names be present or return nothing at all.我不知道如何从同一列返回具有不同结果的多行,并要求存在特定的分支名称或根本不返回任何内容。 (userID = a on one row, b on another row, c on anther row or do not return userID at all.) I do not want the entire answer, I just need to know what function I can use to do this and maybe some general guidance on what to do. (userID = a on one row, b on another row, c on another row or not return userID.)关于做什么的一般指导。

I can provide the table information if needed.如果需要,我可以提供表格信息。

Thank you.谢谢你。

I don't want the complete answer我不想要完整的答案

Good and better to learn.很好,更好地学习。

So I explain here how to do, just let me know what's not clear.所以我在这里解释怎么做,只是让我知道什么不清楚。

I need to find all user ID's that have at least one account in each branch.我需要找到在每个分支中至少有一个帐户的所有用户 ID。

We construst the sql in 3 times:我们对 sql 进行了 3 次构造:

sql1: per City the name of branches sql1:每个城市的分支机构名称

sql2: per ID & City the name of branches sql2:每个 ID 和城市的分支机构名称

sql3: ID from sql1 & sql2 whith same branches sql3:来自具有相同分支的 sql1 和 sql2 的 ID

Go ahead and ask when you need. Go 提前询问您何时需要。

On clue is to be able to have one field with all the branches in order to be able to test it easily.线索是能够拥有一个包含所有分支的字段,以便能够轻松测试它。

See the logic in comment and other answer to get the final solution.请参阅评论中的逻辑和其他答案以获得最终解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM