Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3486310
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:00:53+00:00 2026-05-18T11:00:53+00:00

I’m using three mysql tables that looks like: The table for members accounts |

  • 0

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 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T11:00:54+00:00Added an answer on May 18, 2026 at 11:00 am
    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.