Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Join two tables based on relationship defined in third table

Tags:

sql

join

I have two tables Activity and Action. One or more actions can be performed for an activity. And the relationships between Activity and Action is given in a third table called Activity Action.

How do I retrieve a result set that tells me what action is applicable for each activity using an sql statement? Here's the table structure

Activity Table -ActivityId(PK), ActivityText

Action Table - ActionId(PK), ActionText

ActivityAction -ActivityActionId(PK), ActivityID, ActionID

I want a resultant table in the format

Activity, Applicable Action

(the Activity column should show ActivityText and Applicable Action should show ActionText)

could you please guide me?

Thank you.

like image 398
user176687 Avatar asked Jan 05 '10 20:01

user176687


People also ask

Can we perform join on 3 tables?

It is possible to use multiple join statements together to join more than one table at the same time. To do that you add a second INNER JOIN statement and a second ON statement to indicate the third table and the second relationship.

How do you join two tables based on conditions?

You join two tables by creating a relationship in the WHERE clause between at least one column from one table and at least one column from another. The join creates a temporary composite table where each pair of rows (one from each table) that satisfies the join condition is linked to form a single row.

When joining 3 tables in a select statement how many join conditions are needed in the WHERE clause?

This formula can be extended to more than 3 tables to N tables, You just need to make sure that the SQL query should have N-1 join statement in order to join N tables. for joining two tables, we require 1 join statement and for joining 3 tables we need 2 join statements.


2 Answers

This should do the trick

SELECT Activity.ActivityText as Activity, Action.ActionText as ApplicableAction FROM ActivityAction     INNER JOIN Activity         ON ActivityAction.ActivityId = Activity.ActivityId     INNER JOIN Action          ON ActivityAction.ActionId = Action.ActionId 

You should read up on JOINS in databases. Here is a good starting point:

http://en.wikipedia.org/wiki/Join_%28SQL%29

Basically what we have here is a many to many relationship between Activity and Action which is resolved by two one-to-many relationships using the a join table called ActivityAction.

To get the required data back, we are joining ActivityAction to each one of the tables using the appropriate PK and FK columns and then choosing the string columns in the SELECT

like image 186
Raj More Avatar answered Sep 21 '22 10:09

Raj More


SELECT ActivityText, ActionText FROM Activity JOIN ActivityAction ON Activity.ActivityId = ActivityAction.ActivityId JOIN Action ON ActivityAction.ActionId = Action.ActionId WHERE Activity.ActivityId = 1; 
like image 39
mopoke Avatar answered Sep 23 '22 10:09

mopoke