选择与一项关联的多项

发布于 2024-12-23 03:44:36 字数 655 浏览 1 评论 0原文

我觉得有更好的方法可以做到这一点。我主要只是问我所做的是否正确,或者是否有更好的方法。

观察这些表:

Users
id | name
---+---------
 1 | Andrew
 2 | Greg

Images
id | userid | url
---+--------+-------
 1 |   1    |  img/foo.jpg
 2 |   2    |  img/bar.jpg
 3 |   2    |  img/baz.jpg
 4 |   1    |  img/qux.jpg

如果 Users 表有一个引用图像的外键,则每个用户只能与单个图像关联。但是,我希望允许每个用户与任意数量的图像关联,因此将用户外键放在图像中。

如果用户对存储在另一个表中的数据有一个外键,我了解如何使用 JOIN。但是,在本例中,还有另一个表,其中包含与我感兴趣的用户关联的多个外键。我将如何构建一个 SELECT 查询来检索与该用户关联的所有图像以及所有图像在一个 SELECT 查询中与用户关联的数据(用户类型、电话号码、等等[我已经知道如何做到这一点])?

或者,有更好的方法吗?

编辑 1:

例如,如果您选择 id = 1 的用户,那么它将返回用户的第一行以及图像的第一行和最后一行(因为这些行的 userid = 1)。

I feel like there is a better way to do this. I'm mostly just asking if what I'm doing is right, or if there's a much better way.

Observe these tables:

Users
id | name
---+---------
 1 | Andrew
 2 | Greg

Images
id | userid | url
---+--------+-------
 1 |   1    |  img/foo.jpg
 2 |   2    |  img/bar.jpg
 3 |   2    |  img/baz.jpg
 4 |   1    |  img/qux.jpg

If the Users table had a single foreign key that references Images, each user could only be associated with a single image. However, I'd like to allow each user to be associated with any number of images, hence putting a Users foreign key in Images instead.

I understand how to use JOIN if user has a single foreign key to data stored in another table. However, in this case, there is another table with a number of foreign keys associated with the user I'm interested in. How would I construct a SELECT query that will retrieve all of the images associated with the user in addition to all of the data associated with the user (user type, phone number, blah blah blah [I know how to do this already]) in one SELECT query?

Or, is there a better way to do this?

Edit 1:

For example, if you select the user with id = 1 then it would return the first row of Users and the first and last row of Images (because those rows have userid = 1).

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

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

发布评论

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

评论(2

缱绻入梦 2024-12-30 03:44:36

如果您不希望多行具有相同的(用户)数据,您可以按用户分组并使用 GROUP_CONCAT() 函数将所有 url 收集在一列中:

SELECT u.*
     , GROUP_CONCAT(i.URL)
FROM users u
  LEFT JOIN Images i 
    ON u.ID = i.userID
WHERE u.ID = 1
GROUP BY u.ID

或使用 2 个查询:

SELECT u.*
FROM users u
WHERE u.ID = 1

SELECT i.URL
FROM Images i 
WHERE i.userID = 1

If you do not want multiple rows with identical (user) data you can either group per user and use the GROUP_CONCAT() function to gather all the urls in one column:

SELECT u.*
     , GROUP_CONCAT(i.URL)
FROM users u
  LEFT JOIN Images i 
    ON u.ID = i.userID
WHERE u.ID = 1
GROUP BY u.ID

or use 2 queries:

SELECT u.*
FROM users u
WHERE u.ID = 1

SELECT i.URL
FROM Images i 
WHERE i.userID = 1
枫林﹌晚霞¤ 2024-12-30 03:44:36

所以...如果是 1-1 或 1-M,则连接是相同的。当您想要其中的所有值而另一个中不存在值时,或者您想要两者的所有值(无论是否找到匹配项)时,差异就会发挥作用。我最喜欢的解释 sql 连接逻辑的网站之一是 编码恐怖

Select U.Name, I.URL
FROM users U 
INNER JOIN Images I ON U.ID = I.userID
Where U.ID = 1

将返回

U.Name  I.URL
Andrew  img/foo.jpg
Andrew  img/Qux.jpg

编辑
那么你追求的是A

U.Name  I.URL
Andrew  img/foo.jpg
        img/Qux.jpg   

还是B?

U.Name  I.URL
Andrew  img/foo.jp, img/Qux.jpg

So... If it's a 1-1 or a 1-M the join is the same. Where variances come into play is when you want all of one where values don't exist in the other or you want all of both regardless if matches are found. One of my favorite sites explaining sql join logic is coding horror

Select U.Name, I.URL
FROM users U 
INNER JOIN Images I ON U.ID = I.userID
Where U.ID = 1

would return

U.Name  I.URL
Andrew  img/foo.jpg
Andrew  img/Qux.jpg

EDIT
So what you are after is A

U.Name  I.URL
Andrew  img/foo.jpg
        img/Qux.jpg   

OR B?

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