简体   繁体   English

MySQL联接查询失败

[英]mySQL JOIN query beat down

Alright, I'm not the best with JOIN's and after all these years of working with mySQL you think I would be by now at the least minimally decent. 好吧,对于JOIN来说,我并不是最好的,在与mySQL一起工作了这么多年之后,您认为我现在至少会表现得很体面。 Guess I've never really worked on anything superbly complex til now worth needing to join a table. 猜猜我从来没有真正从事过任何超级复杂的工作,直到现在值得加入一张桌子。 So here I am, confused ever so slightly in need of a helpful example to get me on a roll, something that's pertinent to my actual data that I can make heads or tales of cause all the reading I'm doing online else where just gives me headaches for the moment. 所以在这里,我有点困惑,需要一个有用的例子来引导我前进,这与我的实际数据有关,我可以根据自己的想法或故事来做所有我在线上正在阅读的内容,否则我现在头疼。 I think I might be stuck on the mythology of JOIN's being a hard thing to do, they don't seem like it but when ever I've tried I fail. 我想我可能对JOIN的神话感到固执,这是一件很难的事,他们似乎并不喜欢,但是一旦我尝试过,我都会失败。 So anyway I am working with PHP as my server side coding, and I believe MySQL 5. 所以无论如何,我都将PHP作为服务器端编码,并且相信MySQL 5。

So heres the construct to an extent. 因此在一定程度上继承了这一构架。

I have table information and table connections . 我有表信息和表连接

Connections has: member_id , connection_id , active 连接具有: member_idconnection_idactive

Information has: firstname , lastname , gender , member_id 信息具有: firstnamelastnamegendermember_id

I should say the tables contain more data per table, but as I understand it I need write a query that I can use the member_id as the connector/foreign key. 我应该说这些表在每个表中包含更多数据,但是据我了解,我需要编写一个查询,该查询可以将member_id用作连接器/外键。 Where I can use both sides of the information. 在这里我可以使用双方的信息。 I need to know if active is 1 , and then I need to know all of the columns above mentioned for information. 我需要知道active is 1 ,然后我需要了解上述所有列以供参考。

I tried 我试过了

SELECT member_id, 
    connection_id, 
    active, 
    firstname, 
    lastname, 
    gender, 
    member_id 
FROM connections, information 
WHERE connection.member_id = information.member_id AND 
      connection.active = 1

and I've tried 我已经尝试过

SELECT * FROM connections, information 
WHERE connection.member_id = information.member_id AND 
      connection.active = 1

With the first one I get member_id is ambitious which is understandable to a point i think cause of the matching columns between the two tables. 对于第一个,我得到的member_id雄心勃勃,这在一定程度上是可以理解的,我认为这是两个表之间匹配列的原因。 Then the second query doesn't server me well as it only results with one person. 然后第二个查询不能很好地为我服务,因为它只能由一个人执行。

My Ultimate goal is to find all the connections for a specific member_id in the connections table, while gathering all the information about those connections from the information table using the connection_id as its the same thing as the member_id in the in the information table. 我的终极目标是在连接表中查找特定member_id的所有连接,同时使用connection_id与信息表中的member_id相同的信息从信息表中收集有关那些连接的所有信息。

So in laymans terms if I am not making sense lets say I wanted to list out all my friends in from the DB. 因此,用通俗易懂的话来说,如果我没有道理,可以说我想从数据库中列出我所有的朋友。 My connection table lets me know which people I am connected to where member_id is my id and connection_id is my friends member_id on another table. 我的连接表让我知道与谁连接的人,其中member_id是我的ID,而connection_id是我的朋友member_id在另一个表上。 Hopefully that makes more sense. 希望这更有意义。 And this is where I am having trouble with my query and trying to write it correctly. 这就是我在查询中遇到麻烦并尝试正确编写它的地方。 If I could get a working sane sample of that I think I might be able to make better sense of JOIN's doesnt help that I also can't figure out what type of JOIN is best suited for my needs either I suppose. 如果我可以得到一个合理的示例,我想我也许可以更好地理解JOIN的含义,这也无济于事,我也不知道哪种类型的JOIN最适合我的需求。

EDIT.... 编辑....

Ok as per request from comment below. 确定,根据以下评论的要求。 Sample data expected output from tables that look similar to this: 表看起来像这样的示例数据的预期输出:

Connections Table 连接表

member_id, connection_id, active

1     |    2    |    1

1     |    3    |    1

1     |    4    |    1

1     |    5    |    1

2     |    1    |    1

2     |    5    |    1

3     |    1    |    1

Information Table 信息表

member_id, firstname, lastname, gender, ...other unimportant rows for this query


1    | Chris    |   Something    |    m    | ....

2    | Tony     |   Something    |    m    | ....

3    | Brandon  |   Something    |    m    | ....

4    | Cassie   |   Something    |    f    | ....

5    | Jeff     |   Something    |    m    | ....

6    | John     |   Something    |    m    | ....

now from the connections table I need to gather all the connection_id's associated with my member_id where active is 1 then from the information table gather everyone firstname, lastname, gender.. Now I know I can do this 2 step where I pool all the connection_id's from connections then run them through a loop of some sort and one by one get the resulting id's from the first query but to me that seems a bit obscure and process intensive which I want to avoid, which brings me here.. Currently from my original query posted to the many shared thus far trying to help my results are to the effect of 现在,从连接表中,我需要收集与我的member_id关联的所有connection_id,其中active为1,然后从信息表中,收集每个人的名字,姓氏,性别。。现在,我知道我可以做这2步了,在这里汇集所有的connection_id。连接然后通过某种循环运行它们,并从第一个查询一个接一个地获得结果ID,但对我来说,这似乎有点晦涩难懂,并且需要避免大量的过程,这使我到了这里。发布到到目前为止共享的许多内容,试图帮助我的结果达到

1    |   Chris   |    Something    |    m    |    2    


1    |   Chris   |    Something    |    m    |    3    


1    |   Chris   |    Something    |    m    |    4    


1    |   Chris   |    Something    |    m    |    5 

what I'd like to see returned is something like 我希望看到返回的内容类似于

2    |   Tony     |    Something    |    m    


3    |   Brandon  |    Something    |    m  


4    |   Cassie   |    Something    |    f   


5    |   Jeff     |    Something    |    m 

After looking at this I suppose I would also need to know the friendID column that matches my member_id as well but thats something to figure out after this initial hurdle 看完之后,我想我还需要知道与我的member_id匹配的friendID列,但这就是在最初的障碍之后需要弄清楚的一点

The error is in your WHERE Clause because instead of Connections you wrote it Connection which then the server generates the error. 该错误是在你的WHERE子句,因为代替Connections你写的Connection ,然后服务器生成的错误。

...
WHERE connection.member_id = information.member_id AND 
      connection.active = 1

try this: 尝试这个:

SELECT a.Member_ID, 
       a.FirstName, 
       a.LastName, a.gender,
       b.Connection_ID
FROM Information a INNER JOIN Connections b
        on a.Member_ID = b.Member_ID
WHERE b.`Active` = 1 

UPDATE UPDATE

if that's the case then you will most likely have a SELF JOIN 如果真是这样,那么您很可能会有一个SELF JOIN

SELECT DISTINCT b.Connection_ID,
       c.*
FROM Information a INNER JOIN Connections b ON
        a.Member_ID = b.Member_ID
     INNER JOIN Information c ON
        b.Connection_ID = c.Member_ID
WHERE b.`Active` = 1
      AND a.Member_ID = 'ID HERE' -- If you want to get for specific member

The error in the first is "ambiguous" not ambitious. 第一个错误是“模棱两可”而不是雄心勃勃的。 Ambiguous means that the SQL engine doesn't know which column you are talking about, because you have two columns with the same name (different tables but still). 模糊意味着SQL引擎不知道您在谈论哪一列,因为您有两个具有相同名称的列(不同的表,但仍然)。

You can fix that by specifying the table name along with the column name where you list the columns for select. 您可以通过指定表名以及列出要选择的列的列名来解决此问题。

For example 例如

SELECT connections.member_id, connection_id, active, firstname, lastname, gender, information.member_id FROM connections, information WHERE connections.member_id = information.member_id AND connections.active = 1

The problem with returning only one suggests that there is only one record that matches your query but it's hard to guess. 仅返回一条记录的问题表明,只有一条记录与您的查询匹配,但是很难猜测。

尝试更改为:

SELECT * FROM connections c JOIN information i ON i.member_id = c.member_id WHERE c.active = 1

Try this: 尝试这个:

SELECT *
FROM information
    LEFT OUTER JOIN connections
    ON information.member_id = connections.member_id
WHERE connections.active = 1;

You're joining on the wrong tables. 您加入了错误的表。 You said: 你说:

where member_id is my id and connection_id is my friends member_id on another table 其中member_id是我的ID,connection_id是我的朋友,在另一个表上的member_id

Then, what you have to match is connections.connection_id with information.member_id. 然后,您需要匹配的是connections.connection_id和information.member_id。 The simplest solution is: 最简单的解决方案是:

select c.member_id, c.connection_id, c.active from connections c
join information i on c.connection_id = i.member_id
where c.active = 1 and c.member_id = @yourId

That's all :) 就这样 :)

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

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