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
EDIT 1
Let’s take for now the subquery, for now loose the group by stuff:
Make sure you have indexes on these columns:
And run this query:
Then post back how long performed.