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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:40:30+00:00 2026-05-23T07:40:30+00:00

I have two tables that I need to join. These are: art id |

  • 0

I have two tables that I need to join. These are:

  • art

    id | art
    --------
    1  | A
    2  | B
    3  | C
    4  | D
    5  | E
    6  | F
    7  | G
    8  | H
    9  | I
    

    and

  • Sess

    artid | sessid
    --------------
    1     | 1
    2     | 1
    3     | 1
    4     | 1
    1     | 2
    4     | 2
    5     | 2
    6     | 2
    1     | 3
    2     | 3
    7     | 3
    4     | 3
    

where Sess.artid is a foregin key to art.id.

From the tables above we can see that there are 3 sessions: A,B,C,D, A,D,E,F and A,B,G,D.
I want to get a ranking of the arts that occur along with art A. Something like:

D=3
B=2

How could I form such a query in mysql or postgres?

  • 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-23T07:40:30+00:00Added an answer on May 23, 2026 at 7:40 am

    You need to join twice the session table to get the article sharing the same session.
    Then join one time with article for the filter clause, and another time to get the name of the other article in the other session.

    SELECT aSameSession.art, count(*)
    FROM art a
        INNER JOIN Sess s
            ON a.id = s.artid
        INNER JOIN Sess sSameArticle
                ON sSameArticle.sessid = s.sessid
        INNER JOIN art aSameSession
            ON sSameArticle.artid = aSameSession.id
    WHERE A.art = 'A'
    AND aSameSession.art <> 'A'
    GROUP BY aSameSession.art
    

    Output :

    B   2
    C   1
    D   3
    E   1
    F   1
    G   1
    

    This version could be a little difficult to understand, so here a version just with the ID of the article, which is much more simple :

    SELECT sSameArticle.artid, count(*)
    FROM Sess s
        INNER JOIN Sess sSameArticle
            ON sSameArticle.sessid = s.sessid
    WHERE s.artid = 1
    AND sSameArticle.artid != 1
    GROUP BY sSameArticle.artid
    

    Output :

    2   2
    3   1
    4   3
    5   1
    6   1
    7   1
    

    Adding the name of the article is just cosmetic.

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

Sidebar

Related Questions

Need some help joining these two tables I have two views that looks like
I have a two tables that are Subject and chapter . I need to
I have a database two tables and a linking table that I need a
I have two tables that should be joined together by a foreign key relationship,
Suppose I have two tables that are linked (one has a foreign key to
I have 2 tables that I need to join and select the unique rows
I have the following code that will join two different tables together and provide
In the below code I have two tables. I join these, perform a GROUP
I have two tables that are considered a single entity in my domain model.
I have two tables that I am joining with the following query... select *

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.