Results 1 to 6 of 6

Thread: Problem of total Join

  1. #1
    Join Date
    Nov 2009
    Posts
    42

    Problem of total Join

    Here is my problem: either 2 different tables with a column "Common"
    Code:
    Table1
    X | Y 
    --+--
    1 | A
    2 | B
     
    Table2
    X | Z
    --+--
    2 | C
    3 | D
    I would like to find the SELECT query that gives
    Code:
    X | Y    | Z
    --+------+--
    1 | A    | NULL
    2 | B    | C
    3 | NULL | D
    the query
    Code:
    SELECT * FROM Table1 LEFT JOIN Table2 USING (X);
    gives only
    Code:
    X | Y    | Z
    --+------+--
    1 | A    | NULL
    2 | B    | C
    I tried many things without finding solutions. What is the problem with my total Join in my SQL query?

  2. #2
    Join Date
    Nov 2008
    Posts
    1,192

    Re: Problem of total Join

    MySQL does not include "total" join (FULL JOIN), but you can simulate it with 2 outer joins and UNION like this:
    Code:
    SELECT x, y, z
    FROM table1
      NATURAL LEFT JOIN table2
    UNION
    SELECT x, y, z
    FROM table1
      NATURAL RIGHT JOIN table2

  3. #3
    Join Date
    Nov 2008
    Posts
    1,054

    Re: Problem of total Join

    You have not tried FULL OUTER JOIN:
    Code:
    WITH T1 (X, Y) AS (SELECT 1, 'A' union ALL SELECT 2, 'B')
       , T2 (X, Z) AS (SELECT 2, 'C' union ALL SELECT 3, 'D')
    SELECT
        coalesce(T1.X, T2.X) AS X,
        T1.Y,
        T2.Z
    FROM
        T1 full OUTER JOIN T2
           ON T2.X = T1.X
    ORDER BY
        X ASC;
     
    X           Y    Z
    ----------- ---- ----
    1           A    NULL
    2           B    C
    3           NULL D

  4. #4
    Join Date
    Nov 2008
    Posts
    1,192

    Re: Problem of total Join

    It does not fail to coalesce (), at least not on the X column, since it serves as "pivotal" to the joints. All values in column X are back and are never null, right?

  5. #5
    Join Date
    Nov 2009
    Posts
    42

    Re: Problem of total Join

    In fact, the join column is not null, I had not specified in the hypothesis. I am using MySQL 5.0.83. I thought that we do not have a "FULL JOIN" in MySQL. Thank you for the double join with union, it was not super difficult, however, I think of it!

  6. #6
    Join Date
    May 2008
    Posts
    685

    Re: Problem of total Join

    MySQL allows left outer joins (LEFT [OUTER] JOIN) and right (RIGHT [OUTER] JOIN), but not the full outer join. This join returns all values of the joined tables, filling the columns of a NULL when there is no correlation with the other table.

Similar Threads

  1. Replies: 3
    Last Post: 02-06-2012, 12:44 PM
  2. Medieval 2 Total War Gold install major problem
    By ronjon707 in forum Video Games
    Replies: 7
    Last Post: 06-01-2011, 02:36 PM
  3. Equi Join vs Natural Join in SQL
    By Demetrius in forum Software Development
    Replies: 3
    Last Post: 14-05-2009, 12:55 PM
  4. Steam Problem With Empire Total War Demo
    By Keegan in forum Video Games
    Replies: 4
    Last Post: 17-04-2009, 09:03 PM
  5. Replies: 3
    Last Post: 16-03-2009, 11:43 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,715,086,311.20002 seconds with 16 queries