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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:25:34+00:00 2026-06-03T02:25:34+00:00

I wasn’t even sure how to phrase this question. I’ll give example content and

  • 0

I wasn’t even sure how to phrase this question. I’ll give example content and wanted output, I’m looking for a query to do this.

Let’s say I have table called “flagged” with this content:

content_id | user_id
1          | 1
1          | 2
1          | 3
2          | 1
2          | 3
2          | 4
3          | 2
3          | 3
4          | 1
4          | 2
5          | 1
6          | 1
6          | 4

And I have a a-symmetrical relationship between content_ids:

master_content_id | slave_content_id
1                 | 2
3                 | 4
5                 | 6

For each “master” content_id (1, 3 and 5), I want to count how many distinct users have flagged either the master or the slave content, but count someone who flagged both as a single flag – which means that in the above example, content_id=1 was counted by user_id=1 (as content_id=1 and content_id=2), by user_id=2 (as content_id=1), by user_id=3 (as content_id=1 and content_id=2), and by user_id=4 (as content_id=2!)

An example of the output of the query I want to make is:

content_id | user_count
1          | 4          # users 1, 2, 3, 4
3          | 3          # users 1, 2, 3
5          | 2          # users 1, 4

I can’t assume that the related content_ids are always a consecutive odd/even (i.e. 66 can be the master of the slave 58)

I am using MySQL and don’t mind using its extensions to SQL (but rather the query be ANSI, or at least portable to the most databases)

  • 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-03T02:25:36+00:00Added an answer on June 3, 2026 at 2:25 am

    From the samples given, does this do the job (I don’t have MySQL available to test)?

    SELECT 
        ms.master_content_id,
        (SELECT COUNT(DISTINCT f.user_id) FROM flagged f WHERE
                          f.content_id = ms.slave_content_id OR
                          f.content_id = ms.master_content_id)
    FROM
        master_slave ms
    

    It would be better not to have the DISTINCT, but I can’t see a way around it.

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

Sidebar

Related Questions

I wasn't even sure how to phrase this so you'll have to forgive me
I wasn't sure exactly how to phrase the question Title. I have this block
I wasn't sure how to phrase this so the title is a bit cryptic.
I wasn't sure what to title this question. Here's my goal: On page one,
I wasn't sure how to word this question, so i'll just explain. I want
Wasn't sure how to write a good title for this question... :) I'm new
I wasn't even sure how to title this right. I have a page that
I wasn't sure how to properly name this question, so if its better suited
I wasn't sure how to word the question for this topic...sorry. I'm just starting
I wasn't completely sure how to phrase what I wanted to ask in the

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.