I have two tables that I need to join. These are:
-
artid | art -------- 1 | A 2 | B 3 | C 4 | D 5 | E 6 | F 7 | G 8 | H 9 | Iand
-
Sessartid | 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?
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.
Output :
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 :
Output :
Adding the name of the article is just cosmetic.