Skip to main content
Inspiring
September 2, 2008
Question

joining question

  • September 2, 2008
  • 2 replies
  • 312 views
Wondering if someone could guide me in the direction I need to pull some results. I have a dynamic select box that I only want to pull teams from if they are not involved in a challenge. Question is, I'm not quite sure how to go about pulling this out.

I have a table name "team". Fields: id, captain, teamname

And a table holding the challenges called "teamchallenges" Fields: id, team1, team2, team3, team1active, team2active, team3active

What I want to do is display in this dropdown the id, captain and teamname from "team" ONLY if they are not listed anywhere in the "teamchallenges" table as being active.

The variable "team1" holds the id from the table "team" and then the variable "team1active" is a yes or no. Same goes for the variables 1 - 3.

I am stumped on what kind of coding I need to do this. I think I need some type of inner join.
    This topic has been closed for replies.

    2 replies

    Inspiring
    September 3, 2008

    Something like this should work for you (I have not verified the syntax)

    SELECT * FROM team
    WHERE 1=1
    AND NOT ID IN (SELECT team1 FROM teamchallenges WHERE team1active= true)
    AND NOT ID IN (SELECT team2 FROM teamchallenges WHERE team2active= true)
    AND NOT ID IN (SELECT team3 FROMteamchallenges WHERE team3active= true)

    cheers,
    fober
    Inspiring
    September 2, 2008
    The design of your teamchallenges table is not normalized and this makes simple sql unavailable. Take another look at your business rules and come up with a design that has only 1 team per record in that table.

    Hint - google "many to many relationships"