Joining table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dotNetDummi
    New Member
    • Nov 2007
    • 9

    #1

    Joining table

    I've two tables where their composite keys are foreign keys that references each other.

    table1 (projId, userId, name, add)
    table2 (projId, userId, status, responseDate, reason)

    I need to retrieve table1(name,add ) and table2( status, responseDate, reason) with projId only..

    my query is
    Code:
    Select ap.name, ap.add, p.status,
    p.responseDate, p.reason 
    from table1 ap, table2 p
    where p.projID = ap.projID 
    and ap.userId = p.userId
    and p.projectID = 'test1'
    but the output is like this:

    Name Add Status responseDate Reason
    name1 123 Pending 21-11-2007
    name1 123 Approved 21-11-2007
    name1 123 Rejected 21-11-2007 Invalid name provided


    The last three columns are correct but name and add is not.
    I need my result to be like

    Name Add Status responseDate Reason
    name1 123 Pending 21-11-2007
    name2 456 Approved 21-11-2007
    name3 789 Rejected 21-11-2007 Invalid name provided

    These are my create statements..

    Code:
    create table table1 (
    projectid varchar(10),
    userid int identity(1,1),
    name varchar(50),
    add varchar(20),
    email varchar(80),
    primary key (projectid, apid)
    );
    
    create table table2(
    projectid varchar(10),
    userid int,
    status varchar(15),
    reason varchar(100),
    responseDate varchar(15),
    primary key (projectid,userid),
    constraint fk_table1 foreign key (projectid, apid) references table2(projectid, apid)
    );
    What have I do wrong? Please help. Thanks
    table2 is the parent table. I know that the database design is not good, but my team decided this.
  • amitpatel66
    Recognized Expert Top Contributor
    • Mar 2007
    • 2358

    #2
    Originally posted by dotNetDummi
    I've two tables where their composite keys are foreign keys that references each other.

    table1 (projId, userId, name, add)
    table2 (projId, userId, status, responseDate, reason)

    I need to retrieve table1(name,add ) and table2( status, responseDate, reason) with projId only..

    my query is
    Code:
    Select ap.name, ap.add, p.status,
    p.responseDate, p.reason 
    from table1 ap, table2 p
    where p.projID = ap.projID 
    and ap.userId = p.userId
    and p.projectID = 'test1'
    but the output is like this:

    Name Add Status responseDate Reason
    name1 123 Pending 21-11-2007
    name1 123 Approved 21-11-2007
    name1 123 Rejected 21-11-2007 Invalid name provided


    The last three columns are correct but name and add is not.
    I need my result to be like

    Name Add Status responseDate Reason
    name1 123 Pending 21-11-2007
    name2 456 Approved 21-11-2007
    name3 789 Rejected 21-11-2007 Invalid name provided

    These are my create statements..

    Code:
    create table table1 (
    projectid varchar(10),
    userid int identity(1,1),
    name varchar(50),
    add varchar(20),
    email varchar(80),
    primary key (projectid, apid)
    );
    
    create table table2(
    projectid varchar(10),
    userid int,
    status varchar(15),
    reason varchar(100),
    responseDate varchar(15),
    primary key (projectid,userid),
    constraint fk_table1 foreign key (projectid, apid) references table2(projectid, apid)
    );
    What have I do wrong? Please help. Thanks
    table2 is the parent table. I know that the database design is not good, but my team decided this.
    You create a primary composite key using apid but I dont find that column in your create table statement??

    Comment

    • dotNetDummi
      New Member
      • Nov 2007
      • 9

      #3
      Originally posted by amitpatel66
      You create a primary composite key using apid but I dont find that column in your create table statement??
      er, nono. sorry typo. all apid should be userid..
      the data are working already.

      anyway, i've change my database. no more composite key and it's working. Thanks for your reply though.

      Comment

      • amitpatel66
        Recognized Expert Top Contributor
        • Mar 2007
        • 2358

        #4
        Originally posted by dotNetDummi
        er, nono. sorry typo. all apid should be userid..
        the data are working already.

        anyway, i've change my database. no more composite key and it's working. Thanks for your reply though.
        Good to hear that its working now.
        POST back in case of any further issues!!

        Comment

        Working...