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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:34:15+00:00 2026-05-19T12:34:15+00:00

I have the following scenario: Table 1: articles id article_text category author_id 1 hello

  • 0

I have the following scenario:

 Table 1:

 articles

 id article_text  category  author_id
 1  "hello world"    4                1
 2 "hi"              5                2
 3 "wasup"           4                3


 Table 2

 authors

 id   name     friends_with
 1    "Joe"      "Bob"
 2    "Sue"      "Joe"
 3    "Fred"     "Bob"

I want to know the total number of authors that are friends with “Bob” for a given category.

So for example, for category 4 how many authors are there that are friends with “Bob”.

The authors table is quite large, in some cases I have a million authors that are friends with “Bob”

So I have tried:

Get list of authors that are friends with bob, and then loop through them and get the count for each of them of that given category and sum all those together in my code.

The issue with this approach is it can generate a million queries, even though they are very fast, it seems there should be a better way.

I was thinking of trying to get a list of authors that are friends with bob and then building an IN clause with that list, but I fear that would blow out the amt of memory allowed in the query set.

Seems like this is a common problem. Any ideas?

thanks

  • 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-19T12:34:16+00:00Added an answer on May 19, 2026 at 12:34 pm
    SELECT COUNT(DISTINCT auth.id)
    FROM authors auth
    INNER JOIN articles art ON auth.id = art.author_id
    WHERE friends_with = 'bob' AND art.category = 4
    

    Count(Distinct a.id) is required as articles might hit multiple rows for each author.

    But if you have any control over the database I would use a link table for friends_with as your cussrent solution either have to use a comma seperated list of names which will be disastrous for performance and require a completly different query or each author can only have one friend.

    Friends

    id friend_id

    then the query would look like this

    SELECT COUNT(DISTINCT auth.id)
    FROM authors auth
    INNER JOIN articles art ON auth.id = art.author_id
    INNER JOIN friends f ON auth.id = f.id 
    INNER JOIN authors fauth ON fauth.id = f.friend_id
    WHERE fauth.name = 'bob' AND art.category = 4
    

    Its more complex but will allow for many friends, just remeber, this construct calls for 2 rows in friends for each pair, one from joe to bob and one from bob to joe.

    You could build it differently but that would make the query even more complex.

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

Sidebar

Related Questions

I have the following scenario, I have a table column with the name categoryid
I have the following scenario: a table of projects and a table of persons,
I have the following scenario: 1) A phone book table 2) Multiple ways of
I have the following scenario in zend framework: Data Table of students Table of
I have the following scenario: I have a database with a particular MyISAM table
I have the following scenario: An SQL 2000 database with a table containing the
I have a following scenario in my SQL Server 2005 database. zipcodes table has
Consider the following scenario. I have a table (a stupid_table ) in a schema
I have the following scenario I have a main form as MDI parent and
Due to company constraints out of my control, I have the following scenario: A

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.