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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:53:54+00:00 2026-05-23T14:53:54+00:00

In our database tables we keep a number of counting columns to help reduce

  • 0

In our database tables we keep a number of counting columns to help reduce the number of complex look-up queries. For example, in our users table we have columns for the number of reviews written, photos uploaded, friends, followers, etc. To help make sure these stay in sync we have a script that runs periodically to check and update these counting columns.

I’ve been attempting to write an efficient query that calculates the number of friends a specific user has and haven’t been able to figure out how to do it. In our friend model someone is a friend if they add you as a friend (no confirmation needed), so you have to count the number of unique of people when adding the number of friends you have added plus the number of people who have added you as a friend.

Here are two queries that each work to update the friend count for all the rows in our users table for friendship in a single direction. What I can’t figure out it how to combine them so you get the total number of unique friends a user has:

Users Who Added You As A Friend

UPDATE users 
  JOIN (SELECT cid2, COUNT(*) as c 
         FROM connections 
         JOIN users ON connections.cid1 = users.user_id
        WHERE connection_type = "MM" 
          AND connections.status="A" 
          AND users.status != "D"
     GROUP BY cid2) f ON f.cid2 = users.user_id 
   SET users.friends = f.c
 WHERE users.status != "D";

Users Who You Added As A Friend

UPDATE users u 
  JOIN (SELECT cid1, COUNT(*) as c
          FROM connections 
          JOIN users ON connections.cid1 = users.user_id 
         WHERE connection_type = "MM" 
           AND connections.status = "A" 
           AND users.status != "D" 
      GROUP BY cid1) f ON f.cid1 = users.user_id 
   SET users.friends = f.c
 WHERE users.status != "D";
  • 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-23T14:53:55+00:00Added an answer on May 23, 2026 at 2:53 pm

    Attempt 3…

    UPDATE
      users 
    INNER JOIN
    (
      SELECT
        connections.cid1  AS user_id,
        COUNT(*)          AS total
      FROM
      (
        SELECT cid1, cid2 FROM connections WHERE connection_type = 'MM' AND status = 'A'
        UNION
        SELECT cid2, cid1 FROM connections WHERE connection_type = 'MM' AND status = 'A'
      )
        AS connections
      INNER JOIN
        users
          ON  users.user_id = connections.cid2
          AND users.status != 'D'
      GROUP BY
        connections.cid1
    )
      AS friends
        ON friends.user_id = users.user_id 
    SET
      users.friends = friends.total
    WHERE
      users.status != 'D';
    

    (Other options deleted at OP’s request. See edit history if interested.)

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

Sidebar

Related Questions

In our database tables we keep a number of counting columns to help reduce
I need to make a column unique in one of our database tables, and
We've encountered the following situation in our database. We have table 'A' and table
On our live/production database I'm trying to add a trigger to a table, but
Example: I have two tables in my database called classA and classB , and
Our database server is a SQL 2008 server. My colleagues all have XP service
Our database is about to reach 1000 stored procedures. Although we were wise and
Our database schema is stored in a single file that is intended for production
Our database is currently at 64 Gb and one of our apps started to
We are moving our database server to a bigger box. I have several databases

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.