RE: Advanced SQL

  • From: "Ryan Stevens" <RYSteve@xxxxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Fri, 26 Aug 2011 06:16:25 -0400

Hi, Jeff,

You may also want to try a full outer join rather than either a left or
right outer join.  For example,
 
SELECT Topic.TopicName, Action.ActionName FROM Topic
FULL JOIN Relationship ON Topic.ID = Relationship.TopicID
FULL JOIN Action ON Relationship.ActionID = ActionID

If you need unlinked topics, but not unlinked actions, you could also left
join topic into a subquery made between Relationship and Action, such as

SELECT Topic.TopicName, ActionByTopic.ActionName FROM Topic LEFT JOIN
(SELECT Relationship.TopicID, Action.ActionName FROM Relationship
INNER JOIN Action ON Relationship.ActionID = Action.ID) AS ActionByTopic
ON Topic.ID = ActionByTopic.TopicID

HTH
Ryan Stevens

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Jeff Berwick
Sent: Thursday, August 25, 2011 7:10 PM
To: programmingblind@xxxxxxxxxxxxx
Subject: Advanced SQL

Hi Listers,

I need some sql assistance.  

I have three tables that are connected in a many to many relationship.  The
three tables are:

topic, relationship (the many to many table) and Action.

I want to show all the relationships but, if there are no relationships I
need to still show the Topics.  Sample tables might look like:

Topic
 ++++++
id
+++++
1
2
3
4

Relationship
+++++++
topic_id | action_id
+++++++
1 | 1
3 | 2
4 | 3
4 | 4

Action
++++++
id
+++++
1
2
3
4

I know I need to use a left outer join at some point but, can't get this
figured out.  Any assistance is welcome and appreciated.

TIA,
Jeff

__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind



__________
View the list's information and change your settings at 
//www.freelists.org/list/programmingblind

Other related posts: