Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2004
    Posts
    3

    Unhappy Unanswered: Selecting a Column From Multiple Tables

    Hello,

    Is it possible to get a list of rows from Multiple tables which have the same Column Name. I have 3 tables which comtain similar info
    and I want to get a list of Names
    the structure is

    ID;Name;Address;Phone No.


    I was thinking something along the lines of SELECT Name FROM TABLE1,TABLE2, TABLE3

    But this does not work.

    Is there a nice way of doing this with SQL or should I do code outside the SQL DB

  2. #2
    Join Date
    Nov 2003
    Location
    FRANCE
    Posts
    393

    Re: Selecting a Column From Multiple Tables

    SELECT Table1.Name, Table2.name
    FROM TABLE1,TABLE2, TABLE3

  3. #3
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    I think you want

    SELECT 'Table1' AS Source, ID, Name, Address, [Phone No]
    FROM Table1
    UNION ALL
    SELECT 'Table2' AS Source, ID, Name, Address, [Phone No]
    FROM Table2
    UNION ALL
    SELECT 'Table3' AS Source, ID, Name, Address, [Phone No]
    FROM Table3


    Happy Hunting (and pecking)
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

  4. #4
    Join Date
    Feb 2004
    Posts
    3

    Thanks ..

    Nice one Brett,

    Thats what I needed.. My wording in the question wasn't great. I wanted to get a result with no duplicates and just a list of each ID's and I am using multiple tables. So Now I use a statement :

    SELECT ID FROM BDname.dbo.Table1
    UNION
    SELECT ID FROM BDname.dbo.Table2
    UNION
    SELECT ID FROM BDname.dbo.Table3

    and it works wonderfully.

    Thanks for the help.

    P.s. Can any one recommend and good TSQL programming resources ?

  5. #5
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    Sure...How about this site

    http://www.dbforums.com/forumdisplay.php?forumid=7

    Or this Site

    http://www.sqlteam.com/default.asp

    Or a good book or 2 on a cold dark rainy night (Ahhh the life of a dba)

    http://www.sqlteam.com/store.asp

    BUT...there is NO better method than opening up (and leaving open) QA, BOL and EM and testing things out yourself.....


    GOOD LUCK!

    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

Posting Permissions

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