Unix Technical Forum

Full Outer Join, Merge Result

This is a discussion on Full Outer Join, Merge Result within the MySQL forums, part of the Database Server Software category; --> Hello Internet! Since MySQL doesn't have a native FULL OUTER JOIN, I simulated it, using a LEFT OUTER JOIN, ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 09:11 AM
Whit Nelson
 
Posts: n/a
Default Full Outer Join, Merge Result

Hello Internet!
Since MySQL doesn't have a native FULL OUTER JOIN, I simulated it,
using a LEFT OUTER JOIN, and RIGHT OUTER JOIN, UNIONing the result
together. The result is like this:

id A id B
53 8 53 15
9 7 9 20
55 7 55 16
54 2 NULL NULL
50 2 50 10
47 2 47 11
57 1 NULL NULL
58 1 58 4
NULL NULL 45 4
NULL NULL 41 3

Where some ids have values for both A and B, but some only have one,
with NULL in the other. (Also, NULL in the id field)

I am trying to merge the id fields into one, so I can reference their
respective values.

Can anyone help me with this? Thanks in advance,

-- whit nelson

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 09:11 AM
Paul Lautman
 
Posts: n/a
Default Re: Full Outer Join, Merge Result

Whit Nelson wrote:
> Hello Internet!
> Since MySQL doesn't have a native FULL OUTER JOIN, I simulated it,
> using a LEFT OUTER JOIN, and RIGHT OUTER JOIN, UNIONing the result
> together. The result is like this:
>
> id A id B
> 53 8 53 15
> 9 7 9 20
> 55 7 55 16
> 54 2 NULL NULL
> 50 2 50 10
> 47 2 47 11
> 57 1 NULL NULL
> 58 1 58 4
> NULL NULL 45 4
> NULL NULL 41 3
>
> Where some ids have values for both A and B, but some only have one,
> with NULL in the other. (Also, NULL in the id field)
>
> I am trying to merge the id fields into one, so I can reference their
> respective values.
>
> Can anyone help me with this? Thanks in advance,
>
> -- whit nelson


We might be able to help if you cold actually explain what you want!

How's about showing us the output that you would like, that'd be a good
start!


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 09:11 AM
Whit Nelson
 
Posts: n/a
Default Re: Full Outer Join, Merge Result

On Feb 17, 4:51 pm, "Paul Lautman" <paul.laut...@btinternet.com>
wrote:
> Whit Nelson wrote:
> > Hello Internet!
> > Since MySQL doesn't have a native FULL OUTER JOIN, I simulated it,
> > using a LEFT OUTER JOIN, and RIGHT OUTER JOIN, UNIONing the result
> > together. The result is like this:

>
> > id A id B
> > 53 8 53 15
> > 9 7 9 20
> > 55 7 55 16
> > 54 2 NULL NULL
> > 50 2 50 10
> > 47 2 47 11
> > 57 1 NULL NULL
> > 58 1 58 4
> > NULL NULL 45 4
> > NULL NULL 41 3

>
> > Where some ids have values for both A and B, but some only have one,
> > with NULL in the other. (Also, NULL in the id field)

>
> > I am trying to merge the id fields into one, so I can reference their
> > respective values.

>
> > Can anyone help me with this? Thanks in advance,

>
> > -- whit nelson

>
> We might be able to help if you cold actually explain what you want!
>
> How's about showing us the output that you would like, that'd be a good
> start!


Sure. So let's say this is my input:

id A id B
53 8 53 15
NULL NULL 9 20
55 7 55 16
54 2 NULL NULL

You'll note how there are 2 id fields. Sometimes they match, and
sometimes one of them is NULL. I would like to merge these two rows so
that there is only 1 id field. Such as this:

id A B
53 8 15
9 NULL 20
55 7 16
54 2 NULL

Eventually, I would like to replace the NULL values with zeros, so I
can do some math on them.

-- whit

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 09:11 AM
Whit Nelson
 
Posts: n/a
Default Re: Full Outer Join, Merge Result

On Feb 17, 5:04 pm, "Whit Nelson" <pengypeng...@gmail.com> wrote:
> On Feb 17, 4:51 pm, "Paul Lautman" <paul.laut...@btinternet.com>
> wrote:
>
>
>
> > Whit Nelson wrote:
> > > Hello Internet!
> > > Since MySQL doesn't have a native FULL OUTER JOIN, I simulated it,
> > > using a LEFT OUTER JOIN, and RIGHT OUTER JOIN, UNIONing the result
> > > together. The result is like this:

>
> > > id A id B
> > > 53 8 53 15
> > > 9 7 9 20
> > > 55 7 55 16
> > > 54 2 NULL NULL
> > > 50 2 50 10
> > > 47 2 47 11
> > > 57 1 NULL NULL
> > > 58 1 58 4
> > > NULL NULL 45 4
> > > NULL NULL 41 3

>
> > > Where some ids have values for both A and B, but some only have one,
> > > with NULL in the other. (Also, NULL in the id field)

>
> > > I am trying to merge the id fields into one, so I can reference their
> > > respective values.

>
> > > Can anyone help me with this? Thanks in advance,

>
> > > -- whit nelson

>
> > We might be able to help if you cold actually explain what you want!

>
> > How's about showing us the output that you would like, that'd be a good
> > start!

>
> Sure. So let's say this is my input:
>
> id A id B
> 53 8 53 15
> NULL NULL 9 20
> 55 7 55 16
> 54 2 NULL NULL
>
> You'll note how there are 2 id fields. Sometimes they match, and
> sometimes one of them is NULL. I would like to merge these two rows so
> that there is only 1 id field. Such as this:
>
> id A B
> 53 8 15
> 9 NULL 20
> 55 7 16
> 54 2 NULL
>
> Eventually, I would like to replace the NULL values with zeros, so I
> can do some math on them.
>
> -- whit


Should I consider the COALESCE function? I'm not that familiar with
it, but it looks to handle this type of NULL-value situation ...

-- whit

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 09:11 AM
Paul Lautman
 
Posts: n/a
Default Re: Full Outer Join, Merge Result

Whit Nelson wrote:
> On Feb 17, 4:51 pm, "Paul Lautman" <paul.laut...@btinternet.com>
> wrote:
>> Whit Nelson wrote:
>>> Hello Internet!
>>> Since MySQL doesn't have a native FULL OUTER JOIN, I simulated it,
>>> using a LEFT OUTER JOIN, and RIGHT OUTER JOIN, UNIONing the result
>>> together. The result is like this:

>>
>>> id A id B
>>> 53 8 53 15
>>> 9 7 9 20
>>> 55 7 55 16
>>> 54 2 NULL NULL
>>> 50 2 50 10
>>> 47 2 47 11
>>> 57 1 NULL NULL
>>> 58 1 58 4
>>> NULL NULL 45 4
>>> NULL NULL 41 3

>>
>>> Where some ids have values for both A and B, but some only have one,
>>> with NULL in the other. (Also, NULL in the id field)

>>
>>> I am trying to merge the id fields into one, so I can reference
>>> their respective values.

>>
>>> Can anyone help me with this? Thanks in advance,

>>
>>> -- whit nelson

>>
>> We might be able to help if you cold actually explain what you want!
>>
>> How's about showing us the output that you would like, that'd be a
>> good start!

>
> Sure. So let's say this is my input:
>
> id A id B
> 53 8 53 15
> NULL NULL 9 20
> 55 7 55 16
> 54 2 NULL NULL
>
> You'll note how there are 2 id fields. Sometimes they match, and
> sometimes one of them is NULL. I would like to merge these two rows so
> that there is only 1 id field. Such as this:
>
> id A B
> 53 8 15
> 9 NULL 20
> 55 7 16
> 54 2 NULL
>
> Eventually, I would like to replace the NULL values with zeros, so I
> can do some math on them.
>
> -- whit


You could use 2 COALESCEs but I think IFNULL is better in this case.
Assuming that you have aliased your tables as a and b, you could try:

SELECT IFNULL(a.id,b.id), IFNULL(a.A,0), IFNULL(b.B,0) FROM .....


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-28-2008, 09:11 AM
Whit Nelson
 
Posts: n/a
Default Re: Full Outer Join, Merge Result

On Feb 18, 9:50 am, "Paul Lautman" <paul.laut...@btinternet.com>
wrote:
> Whit Nelson wrote:
> > On Feb 17, 4:51 pm, "Paul Lautman" <paul.laut...@btinternet.com>
> > wrote:
> >> Whit Nelson wrote:
> >>> Hello Internet!
> >>> Since MySQL doesn't have a native FULL OUTER JOIN, I simulated it,
> >>> using a LEFT OUTER JOIN, and RIGHT OUTER JOIN, UNIONing the result
> >>> together. The result is like this:

>
> >>> id A id B
> >>> 53 8 53 15
> >>> 9 7 9 20
> >>> 55 7 55 16
> >>> 54 2 NULL NULL
> >>> 50 2 50 10
> >>> 47 2 47 11
> >>> 57 1 NULL NULL
> >>> 58 1 58 4
> >>> NULL NULL 45 4
> >>> NULL NULL 41 3

>
> >>> Where some ids have values for both A and B, but some only have one,
> >>> with NULL in the other. (Also, NULL in the id field)

>
> >>> I am trying to merge the id fields into one, so I can reference
> >>> their respective values.

>
> >>> Can anyone help me with this? Thanks in advance,

>
> >>> -- whit nelson

>
> >> We might be able to help if you cold actually explain what you want!

>
> >> How's about showing us the output that you would like, that'd be a
> >> good start!

>
> > Sure. So let's say this is my input:

>
> > id A id B
> > 53 8 53 15
> > NULL NULL 9 20
> > 55 7 55 16
> > 54 2 NULL NULL

>
> > You'll note how there are 2 id fields. Sometimes they match, and
> > sometimes one of them is NULL. I would like to merge these two rows so
> > that there is only 1 id field. Such as this:

>
> > id A B
> > 53 8 15
> > 9 NULL 20
> > 55 7 16
> > 54 2 NULL

>
> > Eventually, I would like to replace the NULL values with zeros, so I
> > can do some math on them.

>
> > -- whit

>
> You could use 2 COALESCEs but I think IFNULL is better in this case.
> Assuming that you have aliased your tables as a and b, you could try:
>
> SELECT IFNULL(a.id,b.id), IFNULL(a.A,0), IFNULL(b.B,0) FROM .....


Marvelous! Many thanks.

-- whit

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 05:33 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com