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 952241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:51:43+00:00 2026-05-15T23:51:43+00:00

What I ended up doing was taking two SQL queries and using the array_intersect()

  • 0

What I ended up doing was taking two SQL queries and using the array_intersect() in PHP to filter out the results:

$sql1 = 'SELECT z.*, u.username, u.user_colour, u.username_clean, u.user_avatar, u.user_avatar_type
    FROM ' . ZEBRA_TABLE . ' z, ' . USERS_TABLE . ' u
    WHERE (( z.user_id = ' . $user->data['user_id'] . '
        AND z.friend = 1
        AND u.user_id = z.zebra_id )
            OR ( z.zebra_id = ' . $user->data['user_id'] . '
                AND z.friend = 1
                AND u.user_id = z.user_id ))
    ORDER BY u.username_clean ASC';

$sql2 = 'SELECT z.*, u.username, u.user_colour, u.username_clean, u.user_avatar, u.user_avatar_type
    FROM ' . ZEBRA_TABLE . ' z, ' . USERS_TABLE . ' u
    WHERE (( z.user_id = ' . $user_id . '
        AND z.friend = 1
        AND u.user_id = z.zebra_id )
            OR ( z.zebra_id = ' . $user_id . '
                AND z.friend = 1
                AND u.user_id = z.user_id ))
    ORDER BY u.username_clean ASC';

The structure of both queries are the same and the only difference is $user->data['user_id] (first person) is replaced with $user_id (second person) in the second query. I want to retrieve friends that both users have in common. Could anyone merge this into a single query so that I don’t have to use two queries and call array_intersect()?

  • 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-15T23:51:44+00:00Added an answer on May 15, 2026 at 11:51 pm

    Well, you could always just subquery both:

    $sql = 'SELECT a.* 
        FROM ('.$sql1.') AS a 
        JOIN ('.$sql2.') AS b ON a.user_id = b.user_id AND a.username = b.username';
    

    You may want to add u.user_id to the field list of both queries u.user_id AS u_user_id then change the second join clause from a.username = b.username to a.u_user_id = b.u_user_id…

    EDIT: Now that I really look at it closer, those two queries are almost identical… Why not just do something like this (replace the where clause to this):

    WHERE z.friend = 1 
       AND (
           ( z.user_id = '.$user_id.' AND u.user_id = z.zebra_id )
            OR
           (z.zebra_id = '.$user_id.' AND u.user_id = z.user_id )
       ) AND (
           ( z.user_id = '.$user->data['user_id'].' AND u.user_id = z.zebra_id )
            OR
           (z.zebra_id = '.$user->data['user_id'].' AND u.user_id = z.user_id )
       ) 
    

    That should give you the result of both queries intersected, and be faster since it can optimize better (hopefully)…

    Oh, and they are in different where blocks because there’s a few cases where z.user_id matches $user_id, but z.zebra_id matches $user->data['user_id']… So rather than list all the permutations, I just layed it out like this…

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

Sidebar

Related Questions

I'm using something like this mysite.com/out.php?url=outurl.com I'm just using a simple redirect, but I'm
I tried doing this: [toolbar setTint:[UIColor colorWithPatternImage:[UIImage imageNamed:@thingFromMyBundle.png]]]; but it just ended up black.
This ended up being a path issue. I was using path as one of
This is what I finally ended up doing. It works great but could probably
I need to find the minimum between 3 values, and I ended up doing
I'm creating a site using PHP and it has to access the Barclaycard ePDQ
Okay this is similar to my last question but what I ended up doing
I ended up doing some emergency PM stuff at work and I would like
I ended up doing like this, struct init { CHAR Name[65]; }; void main()
How would you go about proving that two queries are functionally equivalent, eg they

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.