If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > PostgreSQL > Selecting from 3 databases

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-03-11, 07:34
NycNessyness NycNessyness is offline
Registered User
 
Join Date: Jul 2011
Posts: 1
Selecting from 3 databases

Hello Everyone! I've been working on this specific statement for a while now and all my attempts are failing. What I'm basically trying to do is create a statement that shows the names of athletes who are NOT playing in any team belonging to a specific school. There are two schools with four sports each. So I have created tables for Athletes, the sports they play, the number of sports they are involved in and the school they are enrolled in. Below is the statement I came up with but I'm having difficulty with it excluding an athlete that is a part of a team in both schools. In this case I want to show the names of athletes who are NOT playing in any sport that belongs to the North Springs school, so basically joining 4 tables. Any assistance I'll be grateful for so I can create the rest of my database. Thanks!

SELECT A.Name FROM Sport JOIN Playing P ON (P.Sport = S.Id) JOIN Athlete A ON (P.Athlete=A.Id) JOIN School Sc ON (S.School=Sc.Id) WHERE Sc.Name != ‘North Springs’;
Reply With Quote
  #2 (permalink)  
Old 07-03-11, 08:40
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,538
try this --
Code:
SELECT A.Name 
  FROM Athlete A 
LEFT OUTER 
  JOIN Playing P 
    ON P.Athlete = A.Id
LEFT OUTER
  JOIN Sport S
    ON S.Id = P.Sport 
LEFT OUTER
  JOIN School Sc 
    ON Sc.Id = S.School
   AND Sc.Name = 'North Springs'
 WHERE Sc.Name IS NULL
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On