#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    170
    Rep Power
    54

    Sort multiple tables as one (Join but not really)


    Is there a way to sort data from multiple tables as a single query result? Here is an example of what I would like to accomplish:
    Code:
    Table 1
    Link | Entry1 | Entry2 |
    123  |  foo   |  bar   |
    
    Table 2
    Link | Entry3 | Entry4 |
    456  |  foo1  |  bar1  |
    
    Table 3
    Link | Entry5 | Entry6 |
    789  |  foo2  |  bar2  |
    
    
    Result
    Link | Entry1 | Entry2 | Entry3 | Entry4 | Entry5 | Entry6 |
    123  |   foo  |  bar   |        |        |        |        |
    456  |        |        |  foo1  |  bar1  |        |        |
    789  |        |        |        |        |  foo2  |  bar2  |
  2. #2
  3. SQL Consultant
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2003
    Location
    Toronto Canada
    Posts
    27,147
    Rep Power
    4274
    Code:
    SELECT Link
         , Entry1
         , Entry2
         , NULL AS Entry3
         , NULL AS Entry4
         , NULL AS Entry5
         , NULL AS Entry6 
      FROM table1
    UNION ALL
    SELECT Link
         , NULL 
         , NULL 
         , Entry3
         , Entry4
         , NULL 
         , NULL 
      FROM table2
    UNION ALL       
    SELECT Link
         , NULL
         , NULL
         , NULL
         , NULL
         , Entry5
         , Entry6 
      FROM table3

    Comments on this post

    • SecurityDavid agrees : That worked perfectly. Thank you.
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

IMN logo majestic logo threadwatch logo seochat tools logo