Tuesday, November 27, 2012

MySQL FULL JOIN

Here is a quick example on FULL JOIN in MySQL database.
FULL JOIN concept in MySQL is to return rows when there is a match in one of the tables. Further FULL JOIN or FULL OUTER JOIN is UNION of LEFT JOIN and the RIGHT JOIN of the both tables.
Consider that you have following two tables.

Table 1 : Persons

Eg : queries to create and populate the Persons table.

CREATE TABLE Persons
 (P_Id int, Name varchar(1));
 
INSERT INTO Persons
 (P_Id, Name)
VALUES
 (1, 'A'),
 (2, 'B'),
 (3, 'C'),
 (4, 'D'),
 (5, 'E');


Table 2 : Invoice


Eg : queries to create and populate Invoice table.

CREATE TABLE Invoice
 (Id int, P_Id int);
 
INSERT INTO Invoice
 (Id, P_Id)
VALUES
 (111, 3),
 (112, 3),
 (113, 1),
 (114, 1),
 (115, 15);

Now think that we want to join rows of both these tables on the condition of same P_Id. To perform that we need to get the UNION of RIGHT JOIN and the LEFT JOIN of both tables.
Here is the query to FULL JOIN operation.

SELECT Persons.Name, Persons.P_Id, Invoice.Id
FROM Persons
LEFT JOIN Invoice
  ON Persons.P_Id=Invoice.P_Id
UNION
SELECT Persons.Name, Persons.P_Id, Invoice.Id
FROM Persons
RIGHT JOIN Invoice
  ON Persons.P_Id=Invoice.P_Id


Resulting table :


I think that you can understand what happened here.

1. First result of LEFT OUTER JOIN of both tables.


2. Then result of RIGHT OUTER JOIN of both tables.


3. UNION of these two tables means rows when there is a match in one of the tables or FULL JOIN of these two tables.


If you understand the concept try to use following query to FULL JOIN these tables. Same concept and same result.

SELECT Persons.Name, Persons.P_Id, Invoice.Id
FROM Persons
LEFT JOIN Invoice
  ON Persons.P_Id=Invoice.P_Id
UNION
SELECT Persons.Name, Persons.P_Id, Invoice.Id
FROM Invoice
LEFT JOIN Persons
  ON Persons.P_Id=Invoice.P_Id

Thank you for watching. Always your ideas are welcome.
Happy coding!

3 comments:

  1. meme generator
    know your memes
    meme know your
    Look into my web site : meme know

    ReplyDelete
  2. Whilе ѕtatistiсs show the majоrity of canсer of breast patіents are
    women above poѕt-menopausal аge, а good 25 ρercent
    of women below 50 still get the disease. ӏt іs tо relieve the symptοms of adrenаl cаncеr in advanсed stage.
    Their stuԁy еxposed an еvіdent association betωeеn coffee intake and а
    ԁecrease in thе rіѕk of having
    breast ԁiѕease. http://www.sickdaysurf.com/pictures/view-photo/2/645/
    Also visit my web site ::

    ReplyDelete
  3. Barai Samir
    fοг more informаtіon.
    Additiοnal treаtment mеthods cοnsist of massаgе,
    stretches, ultrasounԁ exam and also pоweг muscles
    arouѕal. When the hеаlth or the phyѕical condition of а paгtner is consideгed іn saѵing a mаrriage relationship.
    Fоrget the past and move on forward, evеn though you feеl
    like уou can't live without him or her, the feeling will pass, and you will soon learn that it will all get better in time and all your wounds will heal.
    Look at my site :: Samir Barai Psychology

    ReplyDelete