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

  • SEARCH
  • Home
  • 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 8210391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:03:42+00:00 2026-06-07T10:03:42+00:00

Currently, I have two MySQL tables. First table stores relation between the friend and

  • 0

Currently, I have two MySQL tables.

First table stores relation between the friend and his picture.

TABLE 1

 id  |  pic_id  |  friend_id
----------------------------
 0   |  123     |  84589
 1   |  290     |  11390
 2   |  884     |  84589
 3   |  456     |  84589
 4   |  123     |  11111
 5   |  456     |  22222

TABLE 2

Second table stores more information about the pic…

id   |  pic_id  |  title   |  color  |  detail
----------------------------------------------
0    |  123     | hello    |  black  |  brush
1    |  124     | world    |   red   |  paint
2    |  884     | sample   |  green  |  star

I use JOIN to grab the information I need…

But what if I want to use the pic_id’s that were matched by the above SQL and find OTHER friend_id’s with the same pic_id?

For example, the above SQL command will give me rows 0, 2, and 3 with pic_id’s 123, 884, 456. What SQL command should I use to loop through these pic_id’s and grab associated friend_id’s? I want to end up with 11111 for pic_id 123 and 22222 for pic_id 456.

I have used the following code to accomplish the above. It does not look efficient and is slow though.

$sql = "SELECT b.title, b.color, b.detail
FROM table1 a INNER JOIN table2 b
on a.pic_id = b.pic_id
WHERE friend_id = 84589";

$res = mysqli_query($link,$sql);
    if($res){
        while ($rec = mysqli_fetch_assoc($res)){
            $pic_id.=$rec['pic_id'].",";
            $arr[] = $rec;
        }
    } 

$each_id = explode(',',$pic_id);
    foreach($each_id as $key => $value){
        if ($value){
            $next_sql = "SELECT friend_id FROM table1 WHERE pic_id=".$value;
            $next_res = mysqli_query($link,$next_sql);

            if ($next_res){
                while($next_rec = mysqli_fetch_assoc($next_res)){
                    //do things
                }
            }
        }
    }       

Sorry if this is unclear. Please let me know and I will clarify.

All help is greatly appreciated!

  • 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-06-07T10:03:43+00:00Added an answer on June 7, 2026 at 10:03 am

    Try this (Updated) :

    SELECT a.friend_id, a.pic_id
     FROM table1 a 
      WHERE EXISTS (SELECT 1 FROM table1 t 
              WHERE t.pic_id = a.pic_id AND t.friend_id = 84589)
    

    check this – http://sqlfiddle.com/#!3/91cdf/3

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

Sidebar

Related Questions

Currently, I have two MySQL tables. First table stores relation between the friend and
Consider these two tables stored in a MySQL database. The first stores a list
I have a script in MySQL that creates two tables, the second table references
Currently I have two pages: The first page contains an input form, and the
I have two tables in MySQL 5.1.38. products +----+------------+-------+------------+ | id | name |
Into I have two tables profile and name , the profile table contains some
I'm working on mysql and have two tables with the same schema: preTrial |id|accusedId|articleid|
I have two MySQL database tables: one containing a list of championships and another
I currently have two tables similar to users and programs that are linked through
I have a mysql DB with two tables: messages message_answers I'd like to fetch

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.