Sergio
Sergio

Reputation: 1239

Mysql join three tables problem

I'm using three mysql tables that looks like:

The table for members accounts

|       id     |     name    |   status   |
-------------------------------------------
|       1      |      mike   |      0     |
|       2      |      peter  |      1     |
|       3      |      john   |      1     |
|       4      |      any    |      1     |

The table for list of friends:

|    myid     |    user     |    date    |
------------------------------------------
|     10      |     2       | 2010-01-04 |
|     3       |     10      | 2010-09-05 |
|     4       |     10      | 2010-10-23 | 

The table for users galleries:

|    fotoid     |    userid     |    pic1    |
------------------------------------------
|      101      |       2       |    1.jpg   |
|      102      |       3       |    2.jpg   |
|      103      |       4       |    3.jpg   |

I want to join this three tables and get query result which will list the users that I added to the list of friends and those who have me add to the list at the same time, all these users must have status of '1 'from the members table and display their photos from the gallery table.

In this example, my ID is '10'. In the table for the galleries field 'MyID' represents those users who have added other users to theirs friends while the 'user' is the ID of the added users.

The end result in this example should look like:

|    id    |   name   |   status   |    pic1   |
------------------------------------------------
|    2     |  peter   |     1      |   1.jpg   |
|    3     |  john    |     1      |   2.jpg   |
|    4     |  any     |     1      |   3.jpg   |

How can I do that?

mysql EXPLAIN:

id  |  select_type     |  table  |    type   |   possible_keys   |  key    |   key_len   |    ref    |    rows   |    extra                            
-------------------------------------------------------------------------------------------------------------------------------------------------
1   |  primary         |   g     |   ALL     |   id              |  NULL   |   NULL      |   NULL    |   7925    |  Using where
1   |  primary         |   a     |   eg_ref  |   id              |  id     |   4         |   g.id    |   1       |  Using where
2   |DEPENDENT SUBQUERY|   a2    |   index   |   id              |  NULL   |   NULL      |   NULL    |   90734   |  Using index; Using temporary; Using filesort;
2   |DEPENDENT SUBQUERY|   f     |   index   |   rds_index       |rds_index|   8         |   NULL    |   138945  |  Using where;Using index;Using join buffer 

Upvotes: 0

Views: 400

Answers (1)

Pentium10
Pentium10

Reputation: 207893

SELECT a.id, 
       a.name, 
       a.status, 
       g.pic1 
FROM   accounts a 
       JOIN galleries g 
         ON g.userid = a.id 
WHERE  a.id IN (SELECT a2.id 
                FROM   accounts a2 
                       JOIN friends f 
                         ON ( f.myid = a2.id 
                               OR f.user = a2.id ) 
                WHERE  ( f.myid = 10 
                          OR f.user = 10 ) 
                GROUP  BY a2.id) 
       and a.status = 1 

EDIT 1

Let's take for now the subquery, for now loose the group by stuff:

Make sure you have indexes on these columns:

accounts.id
friends.myid
friends.user

And run this query:

SELECT a2.id 
FROM   accounts a2 
       JOIN friends f 
         ON f.myid = a2.id 
WHERE  f.user = 10 
UNION ALL 
SELECT a2.id 
FROM   accounts a2 
       JOIN friends f 
         ON f.user = a2.id 
WHERE  f.myid = 10 

Then post back how long performed.

Upvotes: 2

Related Questions