Results 1 to 4 of 4

Thread: SQL Join

  1. #1

    Thread Starter
    Hyperactive Member gonzalioz's Avatar
    Join Date
    Sep 2009
    Location
    <body></body>
    Posts
    508

    SQL Join

    Hi all,

    Let's say I have two tables:

    tblComputer
    computername
    description

    tblUser
    username
    password

    A computer can have multiple users.
    A user can only have 1 computer.

    How would a table look like if there were 3 users using 1 computer and I joined them?

    Like this?

    tblJoined
    tblComputer.computername
    tblComputer.description
    tblUser.username
    tblUser.password
    tblUser.username
    tblUser.password
    tblUser.username
    tblUser.password

  2. #2
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: SQL Join

    I think this would be better demonstrated with sample data.

    How do you plan to join tblUser with tblComputer? I don't see a common key.
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  3. #3
    New Member
    Join Date
    Nov 2009
    Posts
    8

    Re: SQL Join

    You will need to restructure your tables a little, like this:

    tblComputer
    tblComputerID (identity field, Primary Key)
    computername
    description

    tblUser
    tblComputerID (Foreign Key, related to tblComputer/tblComputerID)
    username
    password

    The tblUser/tblComputerID points to which computer is associated with which user.

    Then your SQL Statements will have to contain a JOIN on the key

    Make sense?

    Regards,

    Will

  4. #4
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    540

    Re: SQL Join

    vb Code:
    1. SELECT c.[Name],c.Description, u.UserName, u.PassWord
    2. FROM [User] u
    3.     INNER JOIN Computer c on u.computerID = c.ID

    Will produce a result set like so:
    Name Description UName Password
    Comp1 Development Machine JCG 1234
    Comp1 Development Machine MPH aaabbb
    Comp1 Development Machine DRH Zebra
    Where I'm from we only have one bit of advice for new comers: "If you hear banjos, turn and run".


    VS 2008 .NetFW 2.0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width