Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2003
    Posts
    2

    Question Unanswered: Emulating UNION with JOINs

    Is there a way I can emulate a UNION statment by using JOINs? I'm stuck on MySQL 3.23.49 and the db admin won't be upgrading to 4 anytime soon.

    I only need the very simplest union, something like

    Code:
    SELECT a FROM t1 UNION SELECT a FROM t2;
    would do the trick.


    Thanks in advance,
    Ken.

  2. #2
    Join Date
    Jan 2003
    Location
    Paris, France
    Posts
    320

    Re: Emulating UNION with JOINs

    Originally posted by Ken Oath
    Is there a way I can emulate a UNION statment by using JOINs? I'm stuck on MySQL 3.23.49 and the db admin won't be upgrading to 4 anytime soon.

    I only need the very simplest union, something like

    Code:
    SELECT a FROM t1 UNION SELECT a FROM t2;
    would do the trick.


    Thanks in advance,
    Ken.
    you can emulate a UNION by using a temporary table like that

    Create temporary table tablename select (col1 ...., coln ...) from tableA [type=heap]
    insert into tablename select (col1 ...., coln ...) from tableB

    Select * from tablename ....
    Olivier Miossec
    --
    http://www.lasso-developpeur.net/
    --

  3. #3
    Join Date
    Jul 2003
    Posts
    30

    Re: Emulating UNION with JOINs

    try something like:

    select t2.a
    from t2 left join t1
    using (your indexes)
    where t1.a is not null

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    ken, have a look at this article --

    Writing UNION statements in MySQL 3.x

    rudy
    http://r937.com/

  5. #5
    Join Date
    Sep 2003
    Posts
    2
    Thanks for the tips.

    Unfortunately the code gnet suggested won't work in all situations. I understand that it will work only when the rows returned from t1 are a subset of the rows returned from t2.

    The code I've ended up using is:

    create temporary table tmp1 type=heap select a from t1;
    insert into tmp1 select a from t2;
    select distinct a from tmp1;

Posting Permissions

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