#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,650
    Rep Power
    171

    Is UNION the best solution when joing multiple queries?


    So I am gonna need to build a mailing list of 5 different tables from different databases. Looks like onion is the best solution. Am I right to do something like this:
    Code:
    SELECT email,
           name
    FROM   hob.c_members
    UNION
    SELECT emailaddress,
           'NULL' /*NULL as name column does not exist*/
    FROM   hob_m.entries
    UNION
    SELECT email,
           'NULL' /*NULL as name column does not exist*/
    FROM   hbc.directory_listing
  2. #2
  3. SQL Consultant
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2003
    Location
    Toronto Canada
    Posts
    27,118
    Rep Power
    4274
    Originally Posted by zxcvbnm
    Looks like onion is the best solution.
    heh, you said "onion"

    did you want 'NULL' (a string) or NULL (a null)?

    but yeah, pulling emails and names out of multiple tables, and eliminating the duplicates, UNION is good for that
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,781
    Rep Power
    348
    If you know that there never will be the same data in more than one table, using UNION ALL will be faster than UNION because UNION tries to remove duplicates.
    I will not read nor answer questions where the SQL code is messy and not formatted properly using [code] tags.
    http://forums.devshed.com/misc.php?do=bbcode#code

    Tips on how to ask better questions:
    http://tkyte.blogspot.de/2005/06/how-to-ask-questions.html
    http://wiki.postgresql.org/wiki/SlowQueryQuestions
    http://catb.org/esr/faqs/smart-questions.html
  6. #4
  7. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,650
    Rep Power
    171
    Originally Posted by r937
    heh, you said "onion"

    did you want 'NULL' (a string) or NULL (a null)?

    but yeah, pulling emails and names out of multiple tables, and eliminating the duplicates, UNION is good for that
    Hey. I didn't mean onion that wouldn't work in SQL.

    I guess I should remove the sngle quotes from NULL.

    Thanks

IMN logo majestic logo threadwatch logo seochat tools logo