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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:35:05+00:00 2026-05-23T11:35:05+00:00

I have a friend table with two primary columns (uid and fid) and am

  • 0

I have a friend table with two primary columns (uid and fid) and am essentially trying to see if for example if the friend is also friends of the user, so

uid 5 -> fid 6
uid 6 -> fid 5

I had tried to mix and mash SELECT queries to somehow return a deciding result, however cannot seem to find anything. count() will return either one or two columns (two being correct), however PHP’s PDO has no good means to find number of selected (not affected) rows without hackish loops.

(SELECT count(uid) FROM friends WHERE uid = 1 AND fid = 2)
  UNION ALL -- Or plain union
(SELECT count(uid) FROM friends WHERE uid = 2 AND fid = 1)

This of course still returns one or two rows of 0/1 or 0 or 1

Can you think of a way to return either 1 or 0 in MySQL in the first column? or a better way of writing this functionality in to my program? (I try best to avoid hackish things)

  • 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-23T11:35:06+00:00Added an answer on May 23, 2026 at 11:35 am
    SELECT a.uid, a.fid
    FROM friends a
      JOIN friends b
         ON a.fid = b.uid
        AND a.uid = b.fid
    WHERE a.uid = 1
      AND a.fid = 2
    

    To avoid “duplicate” rows.. e.g. 2 rows for every couple of friends, add the condition a.uid < b.uid:

    SELECT a.uid, a.fid
    FROM friends a
      JOIN friends b
         ON a.fid = b.uid
        AND a.uid = b.fid
        AND a.uid < b.uid
    WHERE a.uid = 1
      AND a.fid = 2
    

    So, to count all friendships, use:

    SELECT COUNT(*) AS countAllFrienships
    FROM friends a
      JOIN friends b
         ON a.fid = b.uid
        AND a.uid = b.fid
        AND a.uid < b.uid
    

    So, to count friends of user X, use:

    SELECT a.uid
         , COUNT(*) AS numberOfFriends
    FROM friends a
      JOIN friends b
         ON a.fid = b.uid
        AND a.uid = b.fid
    WHERE a.uid = X
    GROUP BY a.uid
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table called User with two columns, one called visitorId and the
I have a friends table with columns user1 and user2 and a users table
I have a table called 'friends', this contains the columns person1 and person2 .
Say, I have two tables like these: Table group Table user +----+-----------+ +----+----------+------+----------+ |
I have two tables table 1 : id, name, age, gender. table 2(friend) :
Currently, I have two MySQL tables. First table stores relation between the friend and
I have two column table friends table where it has a recode of users
Currently, I have two MySQL tables. First table stores relation between the friend and
I need a table which has two columns: First column have an ID Second
I have two tables: User and User2Friend . I need to get user's friends.

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.