简体   繁体   English

如何从两个表构建sql查询?

[英]How to build sql query from two tables?

Members is a table of persons who are members of a club. 成员是俱乐部成员的表。 Various details of each member are included in this table through various columns such as ID, name, address, email, etc. 该表中通过ID,名称,地址,电子邮件等各种列包含了每个成员的各种详细信息。

Now, any two members of this table can have a relationship. 现在,该表的任何两个成员都可以有关系。 For example, if A, B, C, and D are all members of this table, then A and B can have a relation; 例如,如果A,B,C和D均为该表的成员,则A和B可以具有关系; A can C have a relation; A罐C有关系; B and D can have relations, etc. So, while member A can have a relation with all other members (such as B, C, and D, etc.), he can have a relation with any other member in, either way, ie, it can be AB or BA. B和D可以有关系等。因此,虽然成员A可以与所有其他成员(例如B,C和D等)有关系,但他可以以任何一种方式与任何其他成员有关系,即可以是AB或BA。 For this relationship, a new table Relations has been created. 对于此关系,已创建一个新的表Relations。 This has only 3 columns; 它只有3列; namely, ID, FirstMemberID, SecondMemberID. 即ID,FirstMemberID,SecondMemberID。 Here, both FirstMemberID and SecondMemberID are basically the respective IDs of members (in a relationship) from the Members table. 这里,FirstMemberID和SecondMemberID基本上都是从Members表中的成员的各自ID(在某种关系中)。

Now, I want to construct an SQL query (for MySQL) to select full details (all columns) from the Members table for all those members who have a relationship with a person with ID of XYZ in such a manner that XYZ is the first part of this relationship. 现在,我想构造一个用于MySQL的SQL查询(用于MySQL),从Members表中为与ID为XYZ的人有关系的所有成员选择完整的详细信息(所有列),使得XYZ是第一部分这种关系。 Here XYZ is only the ID of the person and not his name. 此处XYZ只是该人的ID,而不是他的名字。 So, I can this first query: 因此,我可以第一个查询:

SELECT SecondMemberID FROM Relations WHERE FirstMemberID = XYZ;

This query gives multi-results because XYZ has relations with many members. 由于XYZ与许多成员都有关系,因此该查询提供了多个结果。 The other select statement is like this: 另一个select语句是这样的:

SELECT * FROM Members WHERE ID = SecondMemberID;

[Note: Here, SecondMemberID is what is returned from the first query. [注意:SecondMemberID是第一个查询返回的内容。 But, as I said, there can be many SecondMemberID here.] 但是,正如我说的,这里可以有很多SecondMemberID。]

My problem is how to combine the two queries in such a way that I get proper results. 我的问题是如何以得到适当结果的方式组合两个查询。

I have tried various methods, like treating the results of the first query as an array and then using IN operator in the second query by using the implode method of PHP or otherwise. 我尝试了各种方法,例如将第一个查询的结果视为数组,然后通过使用PHP的implode方法在第二个查询中使用IN运算符。 I have also tried various JOIN statements but without success. 我也尝试了各种JOIN语句,但没有成功。

What am I missing? 我想念什么? Can you please help? 你能帮忙吗?

Try 尝试

SELECT Members.* 
  FROM Relations LEFT JOIN Members 
    ON (Relations.SecondMemberID=Members.ID) 
 WHERE Relations.FirstMemberID = XYZ;

You need a simple JOIN command in SQL. 您需要使用SQL中的简单JOIN命令。 JOIN joins two tables. JOIN连接两个表。 INNER JOIN joins them on a specific condition, ie concatenates two rows (one from each table) that match the condition. INNER JOIN在特定条件下将它们联接,即,将匹配条件的两行(每个表中的每一行)连接起来。 Condition is usually something like: table1.field1 == table2.field2 or you can use LEFT or RIGHT JOIN which will, in turn, join two rows but the right or left one can be empty. 条件通常是这样的: table1.field1 == table2.field2或者您可以使用LEFT或RIGHT JOIN,它们将依次联接两行,但左或右行可以为空。

I think you want: 我想你要:

SELECT SecondMemberID 
  FROM Relations 
 WHERE Id IN (SELECT SecondMemberID 
                FROM Members
               WHERE FirstMemberID = 'XYZ');

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

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