MySQL 在查询中使用另一个查询的结果?

发布于 2024-12-10 12:57:46 字数 1573 浏览 0 评论 0原文

我需要一些帮助来为 mySQL 数据库创建查询。我最近开始使用 JOIN 和 IN 从数据库中选择行,所以我很抱歉听起来像个菜鸟。

我并不是在寻找答案(尽管有一个答案就更好了!),但是关于我应该从哪里开始查询的建议将不胜感激。请不要仅发布 PHP 网站或教程的链接。

在我的数据库中,用户是“链接的”,顺序并不重要。

表名称:UserLinks

link_ID   User_1   User_2
1         10982*   34982
2         82738    16643
3         10982*   99822
4         78256    10982*

我在这里关注的用户是用户 10982

1) 我要创建的此查询的第一部分查找该用户 (10982) 链接的用户的 ID到。这些用户是用户 34982、99822 和 78256。我想这就像 SELECT * FROM UserLinks WHERE User_1 OR User_2 = 10982 一样简单,我的问题是,如何获取值 - 给定我不能简单地选择要返回的行(我考虑使用 if 语句...如果 User_1 的值为 10982,则选择 User_2。这但是,似乎多余。

有了这个用户 ID 列表,我想运行查询的第二部分,以查找与此用户列表相对应的 Event_ID(用户 ID 现在是 UserEv_ID):

表名称:EventUserTags

ETag_ID   UserEv_ID   Event_ID
1         34982*      289
2         82738       231
3         99822*      990
4         78256*      486

2) 该查询的第二部分将使用第一个查询(用户 ID 的)生成的列表来生成 Event_ID 的列表。我知道您可以使用 IN 语句并将此列表作为数组转储到查询中,但这意味着将查询第一部分的结果放入数组中。这又显得多余了。我想知道如何使用用户 ID 正确选择这些 Event_ID ...我认为 JOIN 查询会起作用,但我需要一些关于如何使用它的建议。

此处获得的 Event_ID 的值是 289、990 和 486。在此查询的最后部分中,我需要使用上次查询生成的 Event_ID 列表来与另一个表(我的 Events 表)的数据进行匹配。

表名称:Events

Event_ID  Event_Order  
182         8728342    
289         3478792*    
990         1876623*     
486         9617789**     

3) 最后,我需要使用上次查询得到的Event_ID 来获取其对应的Even_Order。再说一遍,我知道这可以通过 IN 语句(使用数组)来完成,但这根本效率不高。

此查询的目的是从单个用户的 ID 开始,并找到该用户链接到的每个用户的 Event_Order。

非常感谢任何帮助

I need some help creating a query for my mySQL database. I have recently started using JOIN and IN to select rows from my database, so I apologize for sounding like a noob.

I am not looking for an answer (although one would be nice!) but advice on where I should start with my query would be greatly appreciated. Please do not just post a link to the PHP website or to a tutorial.

In my database, users are "linked", the order is NOT important.

Table Name: UserLinks

link_ID   User_1   User_2
1         10982*   34982
2         82738    16643
3         10982*   99822
4         78256    10982*

The user that I am focusing on here is user 10982

1) The first part of this query that I would like to create find the ID of the users that this user (10982) is linked to. These users are user 34982, 99822, and 78256. I imagine it is something as simple as SELECT * FROM UserLinks WHERE User_1 OR User_2 = 10982, my issue here is, how do I obtain the values - given that I cannot simply choose a row to return (I considered using an if statement... if the value of User_1 is 10982, then choose User_2. This however, seems redundant.

With this list of Users ID's, I would like to run the second part of the query, to find which Event_ID's correspond to this list of users (the ID's of the users are now UserEv_ID):

Table Name: EventUserTags

ETag_ID   UserEv_ID   Event_ID
1         34982*      289
2         82738       231
3         99822*      990
4         78256*      486

2) The second part of this query will use the list generated from the first query (of user ID's) to generate a list of Event_ID's. I know that you can use the IN statement and just dump this list in to the query as an array, but that means making the results of the first part of the query into an array. This again, seems redundant. I would like to know how to properly select these Event_ID's using the User ID's... I think that the JOIN Query will work, but I need some advice on how to use this.

The values of Event_ID's obtained here are 289, 990, and 486. In last part of this query I need to use this list of Event_ID's generated from the last query to match up with the data of another table, my Events table.

Table Name: Events

Event_ID  Event_Order  
182         8728342    
289         3478792*    
990         1876623*     
486         9617789**     

3) Lastly, I need to use the Event_ID's obtained from the last query to obtain their corresponding Even_Order. Again, I know this can be done with the IN statement (using an array) but this will not be efficient at all.

The purpose of this query is to start with a single User's ID, and find the Event_Order of every user this user is linked to.

Any help is really appreciated

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

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

发布评论

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

评论(1

难得心□动 2024-12-17 12:57:46

这一切都可以在一个查询中相当简单地完成,我将编写该查询,然后进行解释:

SELECT
   Event_Order, -- I think this is the one you need, but selecting other fields anyway
   Event_ID,
   User_1,
   User_2
FROM
   Events
   NATURAL JOIN EventUserTags
   JOIN UserLinks ON (
      UserEv_ID = User_1 AND User_2 = 10982
      OR UserEv_ID = User_2 AND User_1 = 10982
   )

NATURAL JOINJOIN EventUserTags ON (Events.Event_ID = EventUserTags.Event_ID) 的快捷方式>。它之所以有效,是因为您要加入的键具有相同的名称。

然后,仅当链接包含您要查找的用户时,您才加入相应的用户 ID。

This can all be done fairly simply in one query which I will write and then explain:

SELECT
   Event_Order, -- I think this is the one you need, but selecting other fields anyway
   Event_ID,
   User_1,
   User_2
FROM
   Events
   NATURAL JOIN EventUserTags
   JOIN UserLinks ON (
      UserEv_ID = User_1 AND User_2 = 10982
      OR UserEv_ID = User_2 AND User_1 = 10982
   )

NATURAL JOIN is a short cut for JOIN EventUserTags ON (Events.Event_ID = EventUserTags.Event_ID). It works because the keys you are joining on have the same name.

Then you join on the respective user IDs only if the link has the user you're looking for.

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