简体   繁体   中英

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. 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. So anyway I am working with PHP as my server side coding, and I believe MySQL 5.

So heres the construct to an extent.

I have table information and table connections .

Connections has: member_id , connection_id , active

Information has: firstname , lastname , gender , member_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. 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.

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. 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.

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. 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.

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

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

The error is in your WHERE Clause because instead of Connections you wrote it Connection which then the server generates the error.

...
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

if that's the case then you will most likely have a 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).

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

Then, what you have to match is connections.connection_id with 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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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