如何从 MySQL 中的一个到多个相关表中选择唯一的行?

发布于 2024-11-27 15:35:08 字数 1242 浏览 0 评论 0原文

作为 MySQL-Noob,我正在努力解决以下任务:假设 3 个表:location groceriesperson 具有以下属性:

  1. 每个表都有一个主自动增量整数 id 和一个 varchar 列。
  2. 表中的条目都是唯一的。
  3. groceries 中的每个条目都有一个 person 中条目的外键。
  4. 来自 groceries 的多个条目可能共享相同的外键。
  5. (3) 和 (4) 也适用于 personlocation

所以我们有一个多对一多的关系。如何选择每个三元组 (groceries_product, person_name, location_name) ,其中 person_name 不会出现多次?

示例:

tables:  groceries                  | person      | location
------------------------------------ ------------- -------------------------
columns: id  product      person_id | id  name    | id  name  person_id
------------------------------------ ------------- -------------------------
         1   hamburger    1         | 1   Peter   | 1   home  1
         2   cheeseburger 1         | 2   Tom     | 2   work  1
         3   carot        1         |             | 3   zoo   2 
         4   potatoe      1         |             |
         5   mango        2         |             |

您可以创建的所有出现 Peter 的三元组都是无关紧要的。我只想要像(芒果,汤姆,动物园)这样的三元组,因为汤姆在所有可能性中只出现一次。我希望我的问题可以理解。 :-)

As MySQL-Noob I am struggeling to solve the following task: Assuming 3 tables: location groceries and person with the following properties:

  1. Every table has a primary autoincremented integer id and a varchar column.
  2. The entrys in the tables are all unique.
  3. Every entry in groceries has a foreign key of an entry in person.
  4. It is possible that more then one entry from groceries share the same foreign key.
  5. (3) and (4) apply to person and location, too

So we have a many to one to many relation. How can I select every triple (groceries_product, person_name, location_name) where person_name does not occur more then once?

Example:

tables:  groceries                  | person      | location
------------------------------------ ------------- -------------------------
columns: id  product      person_id | id  name    | id  name  person_id
------------------------------------ ------------- -------------------------
         1   hamburger    1         | 1   Peter   | 1   home  1
         2   cheeseburger 1         | 2   Tom     | 2   work  1
         3   carot        1         |             | 3   zoo   2 
         4   potatoe      1         |             |
         5   mango        2         |             |

All the triples you can create in which Peter occures are irrelevant. I want only triples like (mango, Tom, zoo), because Tom does occure only once in all possibilities. I hope my question ist understandable. :-)

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

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

发布评论

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

评论(2

慕烟庭风 2024-12-04 15:35:08

我认为你必须进行子选择才能得到结果:

SELECT groceries.product, person.name, location.name
FROM person
LEFT JOIN groceries ON person.id = groceries.person_id
LEFT JOIN location ON person.id = location.person_id
WHERE person.id
IN (
SELECT person.id
FROM person
LEFT JOIN groceries ON person.id = groceries.person_id
LEFT JOIN location ON person.id = location.person_id
GROUP BY person.id
HAVING count( person.id ) =1
)

I think you have to do a subselect to get your result:

SELECT groceries.product, person.name, location.name
FROM person
LEFT JOIN groceries ON person.id = groceries.person_id
LEFT JOIN location ON person.id = location.person_id
WHERE person.id
IN (
SELECT person.id
FROM person
LEFT JOIN groceries ON person.id = groceries.person_id
LEFT JOIN location ON person.id = location.person_id
GROUP BY person.id
HAVING count( person.id ) =1
)
一杆小烟枪 2024-12-04 15:35:08
select l.name, m.name, r.name
from `left` l
left join middle m on m.id = l.middle_id
left join `right` r on m.id = r.middle_id;
select l.name, m.name, r.name
from `left` l
left join middle m on m.id = l.middle_id
left join `right` r on m.id = r.middle_id;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文