简体   繁体   English

MySQL在查询中使用其他查询的结果吗?

[英]MySQL In query using results from another query?

I need some help creating a query for my mySQL database. 我需要一些帮助来为mySQL数据库创建查询。 I have recently started using JOIN and IN to select rows from my database, so I apologize for sounding like a noob. 我最近开始使用JOIN和IN从数据库中选择行,因此对于听起来像菜鸟一样道歉。

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. 请不要仅将链接发布到PHP网站或教程。

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 我在这里关注的用户是用户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. 1)我要创建的此查询的第一部分是查找与此用户(10982)链接的用户的ID。 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. 这些用户是用户34982、99822和78256。我想这就像SELECT * FROM UserLinks WHERE User_1 OR User_2 = 10982一样简单,这里的问题是,如何获得值-鉴于我不能简单地选择一行返回(我考虑过使用if语句...如果User_1的值为10982,则选择User_2。但是,这似乎多余。

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): 使用此用户ID列表,我想运行查询的第二部分,以查找与该用户列表相对应的Event_ID(用户的ID现在为UserEv_ID):

Table Name: EventUserTags 表名称: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. 2)此查询的第二部分将使用从第一个查询(用户ID的查询)生成的列表来生成Event_ID的列表。 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. 我知道您可以使用IN语句并将此列表作为数组转储到查询中,但这意味着将查询的第一部分的结果放入数组中。 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. 我想知道如何使用用户ID来正确选择这些Event_ID。我认为JOIN查询将起作用,但是我需要一些有关如何使用它的建议。

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. 在这里获得的Event_ID的值为289、990和486。在此查询的最后部分中,我需要使用从上一个查询生成的Event_ID的此列表与另一个表(即“事件”表)的数据匹配。

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. 3)最后,我需要使用从上一次查询获得的Event_ID来获取其对应的Even_Order。 Again, I know this can be done with the IN statement (using an array) but this will not be efficient at all. 同样,我知道可以使用IN语句(使用数组)完成此操作,但这根本没有效率。

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. 该查询的目的是从单个用户的ID开始,并找到该用户链接到的每个用户的Event_Order。

Any help is really appreciated 任何帮助都非常感谢

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) . NATURAL JOINJOIN 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. 然后,仅当链接包含您要查找的用户时,您才加入相应的用户ID。

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

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