locked
SQL Statement - Retrieve columns from multiple tables RRS feed

  • Question

  • I have 2 tables who are linked through a primary - foreign key.

    Both tables contain e-mailadresses. I need a SQL Statement that retrieves all the e-mailadresses from both tables.

     

    Thanks in advance,

     

    Davy

    Monday, December 1, 2008 12:09 PM

Answers

  • Not sure if i'm on correct track on or not...

    but try this

    Code Snippet

     

    SELECT IntervenersId, email

    FROM Interveners

    UNION

    SELECT IntervenersId, email

    FROM [Interveners sub contractors]

     

     

     

    Monday, December 1, 2008 4:26 PM

All replies

  • Ok, what is the primary key and foreign key column in your tables?  is it e-mailadresses or something else?
     
     

     
    Monday, December 1, 2008 12:18 PM
  • The primary - foreign key columns aren't the e-mailadresses column. These are just two columns in the tables.

    In your code snippet what does the A and B stand for?

     

    Best regards,

     

    Davy

     

    Monday, December 1, 2008 2:17 PM
  •  

    Please provide table structure and sample data, other wise I'll end up giving you some random solutions

     

    Code Snippet

    SELECT A.PkId, A.[e-mailadresses], B.[e-mailadresses]

    FROM table_1 A

    INNER JOIN

    table_2

    ON A.PkId= B.FkId

     

     

     

    A and B are nothing but the Table alias i assigned to Table_1 and Table_2 respectively.  You can use any word as table alias.
    Monday, December 1, 2008 2:24 PM
  • Table1 (Interveners):

    Column 1 - IntervenersId (primary key)

    Column 2 - Email

    1 - test@test.net

    2 - test2@test.net

    ...

     

    Table 2 (Interveners sub contractors):

    Column 1 - Id (primary key)

    Column 2 - IntervenersId (foreign key)

    Column 3 - Email

     

    1 - 1 - test3@test.net

    2 - 1 - test4@test.net

    3 - 2 - test5@test.net

     

    So record 1 & 2 from table 2 are linked to record 1 from table 1 and record 3 from table 2 is linked to record 2 from table 1 with the primary - foreign key pair IntervenersId. I want to fill up a multiple choice list box with all the e-mailadresses.

    So all the e-mailadresses from both tables should be retrieved with a SQL statement. Is this possible?

     

    Monday, December 1, 2008 3:56 PM
  • Not sure if i'm on correct track on or not...

    but try this

    Code Snippet

     

    SELECT IntervenersId, email

    FROM Interveners

    UNION

    SELECT IntervenersId, email

    FROM [Interveners sub contractors]

     

     

     

    Monday, December 1, 2008 4:26 PM