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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:30:14+00:00 2026-06-05T05:30:14+00:00

I have a MySQL database with 3 tables: users, posts, and users_posts. The users-posts

  • 0

I have a MySQL database with 3 tables: users, posts, and users_posts. The users-posts relationship is many-to-many. How can I get a list of all pairs of posts and how many users they have in common? It essentially gives me a list of posts and how many users have commented on both. The point is to have data ready to import into network analysis software. The resulting list is called an “edgelist” in network terms, the users in common is an edge weight.

Schema:

users
id
name

posts
id
title
body

users_posts
user_id
post_id

Desired output:

postname1         postname2     users_in_common
Here's a title    Title #2      2
Another post      Title #2      11

I tried searching but didn’t even know the right search terms among many-to-many, join, three tables, count, pairs, shared, etc. Thanks for your help!

  • 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-05T05:30:16+00:00Added an answer on June 5, 2026 at 5:30 am

    My query produces all pairs of posts including those without common users (will have 0 in users_in_common in such case). Try my solution on SQL Fiddle or see the code:

    select
        p1.title as postname1,
        p2.title as postname2,
        coalesce(s.users_in_common, 0) as users_in_common
    from posts p1
        inner join posts p2 on p1.id < p2.id
        left join (
            select
                up1.post_id as post1_id,
                up2.post_id as post2_id,
                count(*) as users_in_common
            from users_posts up1, users_posts up2
            where up1.user_id = up2.user_id
                and up1.post_id < up2.post_id
            group by up1.post_id, up2.post_id
        ) s
        on (s.post1_id = p1.id and s.post2_id = p2.id)
    order by p1.id, p2.id;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a mySQL database with a 'users' table which includes a username,
I have a MySQL database with multiple tables, each of which have the same
I have a MySQL database with 60 tables. Most of the tables have primary
I have a MySQL database with a few (five to be precise) huge tables.
I have a MySQL database set-up in a hierarchy style. There are 4 tables
I have created a MySQL database using MySQL Workbench. It has about 20 tables.
In a MySQL database I have two tables linked in a join. One table
I have two tables in a MySql database: COUNTRY --------------- id, country_name and CITY
I have multiple tables in a mysql database and I am trying to put
I have 2 tables in my mysql database: CREATE TABLE IF NOT EXISTS `RECIPES`

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.