<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.2.2" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: How to simulate FULL OUTER JOIN in MySQL</title>
	<link>http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/</link>
	<description>Stay curious!</description>
	<pubDate>Fri, 29 Aug 2008 05:05:27 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.2</generator>

	<item>
		<title>By: Xaprb</title>
		<link>http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/#comment-14289</link>
		<author>Xaprb</author>
		<pubDate>Wed, 12 Mar 2008 23:53:06 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/#comment-14289</guid>
		<description>It turns out that FULL OUTER JOIN isn't possible no matter what.  The reasons are kind of technical.

You should be able to get the columns in the order you want if you specify them explicitly.</description>
		<content:encoded><![CDATA[<p>It turns out that FULL OUTER JOIN isn&#8217;t possible no matter what.  The reasons are kind of technical.</p>
<p>You should be able to get the columns in the order you want if you specify them explicitly.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mark</title>
		<link>http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/#comment-14288</link>
		<author>Mark</author>
		<pubDate>Wed, 12 Mar 2008 23:41:09 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/#comment-14288</guid>
		<description>Thanks for a useful post.  Am I right that a full outer join implementation is not possible using MySQL's NATURAL joins?  I'm struggling to get a result where the column joined on does not appear twice.  NATURAL joins achieve this but return the columns in different orders... any ideas?</description>
		<content:encoded><![CDATA[<p>Thanks for a useful post.  Am I right that a full outer join implementation is not possible using MySQL&#8217;s NATURAL joins?  I&#8217;m struggling to get a result where the column joined on does not appear twice.  NATURAL joins achieve this but return the columns in different orders&#8230; any ideas?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Garry</title>
		<link>http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/#comment-14243</link>
		<author>Garry</author>
		<pubDate>Fri, 22 Feb 2008 14:29:14 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/#comment-14243</guid>
		<description>Its Wonderful! Explanation is best ......... even newbies like me can understand this better.
Thanks a lot!</description>
		<content:encoded><![CDATA[<p>Its Wonderful! Explanation is best &#8230;&#8230;&#8230; even newbies like me can understand this better.<br />
Thanks a lot!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan</title>
		<link>http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/#comment-14148</link>
		<author>Dan</author>
		<pubDate>Sat, 12 Jan 2008 00:01:35 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/#comment-14148</guid>
		<description>Awesome article, helped me big time.

-Dan</description>
		<content:encoded><![CDATA[<p>Awesome article, helped me big time.</p>
<p>-Dan</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris Chou&#8217;s Homeworld &#187; 在主键相关的查询中，模拟MySQL的全外联接</title>
		<link>http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/#comment-13563</link>
		<author>Chris Chou&#8217;s Homeworld &#187; 在主键相关的查询中，模拟MySQL的全外联接</author>
		<pubDate>Tue, 23 Oct 2007 12:07:28 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/#comment-13563</guid>
		<description>[...] http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/ [...]</description>
		<content:encoded><![CDATA[<p>[&#8230;] <a href="http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/" rel="nofollow">http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/</a> [&#8230;]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Xaprb</title>
		<link>http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/#comment-6467</link>
		<author>Xaprb</author>
		<pubDate>Thu, 10 May 2007 12:03:18 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/#comment-6467</guid>
		<description>&lt;p&gt;The MySQL manual explains what you need to do in the section on &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/union.html" rel="nofollow"&gt;UNION syntax&lt;/a&gt;.&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>The MySQL manual explains what you need to do in the section on <a href="http://dev.mysql.com/doc/refman/5.0/en/union.html" rel="nofollow">UNION syntax</a>.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kelmer</title>
		<link>http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/#comment-6465</link>
		<author>kelmer</author>
		<pubDate>Thu, 10 May 2007 06:58:43 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/#comment-6465</guid>
		<description>&lt;p&gt;I tried this:&lt;/p&gt;

&lt;pre&gt;(select * from apples as a
   left outer join oranges as o on a.price= o.price)
union all
(select * from applesas a
   right outer join oranges as o on a.price= o.price
where a.price is null)
order by a.price&lt;/pre&gt;

&lt;p&gt;and I get an error saying: "#1250 - Table 'a' from one of the SELECTs cannot be used in global ORDER clause "

If I use this instead&lt;/p&gt;

&lt;pre&gt;select * from apples as a
   left outer join oranges as o on a.price = o.price
union all
select * from apples as a
   right outer join oranges as o on a.price = o.price
where a.price is null
order by a.price&lt;/pre&gt;

&lt;p&gt;I get this:

"#1109 - Unknown table 'a' in order clause "


Where and how should I puit the ORDER BY sentence?&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>I tried this:</p>
<pre>(select * from apples as a
   left outer join oranges as o on a.price= o.price)
union all
(select * from applesas a
   right outer join oranges as o on a.price= o.price
where a.price is null)
order by a.price</pre>
<p>and I get an error saying: &#8220;#1250 - Table &#8216;a&#8217; from one of the SELECTs cannot be used in global ORDER clause &#8220;</p>
<p>If I use this instead</p>
<pre>select * from apples as a
   left outer join oranges as o on a.price = o.price
union all
select * from apples as a
   right outer join oranges as o on a.price = o.price
where a.price is null
order by a.price</pre>
<p>I get this:</p>
<p>&#8220;#1109 - Unknown table &#8216;a&#8217; in order clause &#8220;</p>
<p>Where and how should I puit the ORDER BY sentence?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Xaprb</title>
		<link>http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/#comment-6455</link>
		<author>Xaprb</author>
		<pubDate>Thu, 10 May 2007 02:25:54 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/#comment-6455</guid>
		<description>&lt;p&gt;You can just add ORDER BY as normal at the end of the query.&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>You can just add ORDER BY as normal at the end of the query.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kelmer</title>
		<link>http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/#comment-6451</link>
		<author>kelmer</author>
		<pubDate>Wed, 09 May 2007 20:23:00 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/#comment-6451</guid>
		<description>&lt;p&gt;Hi,

Using your method, how can I use an ORDER BY sentence to order the rows by the common attribute (in your case, price)?&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>Using your method, how can I use an ORDER BY sentence to order the rows by the common attribute (in your case, price)?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matthew Montgomery</title>
		<link>http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/#comment-3374</link>
		<author>Matthew Montgomery</author>
		<pubDate>Thu, 25 Jan 2007 17:05:50 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/#comment-3374</guid>
		<description>&lt;p&gt;For anyone curious, method #1 CAN be extended to handle more than 2 tables... It just gets ugly.  Basically, for each table LEFT JOIN all other tables to it,  Then union each of those together.&lt;/p&gt;

&lt;pre&gt;CREATE TABLE `table1` (
  `f1` int(11) default NULL,
  `gid` int(11) default NULL
) ENGINE=MyISAM;

CREATE TABLE `table2`...

CREATE TABLE `table3`...

INSERT INTO table1 VALUES(1,1), (4,1), (7,1), (10,3), (13,4), (16,4), (19,4), (22,6);
INSERT INTO table2 VALUES(2,1), (5,2), (8,2), (11,3), (14,4), (17,5), (20,5), (23,6);
INSERT INTO table3 VALUES(3,2), (6,2), (9,3), (12,3), (15,5), (18,5), (21,6), (24,6);&lt;/pre&gt;

&lt;p&gt;Here I'm trying to get the SUM of each group of numbers within a single table and display them together in the same result so I need a subquery here... i.e. table1, group 1 is 1+4+7 = 12; table3, group 2 is 3+6 = 9 ....&lt;/p&gt;

&lt;pre&gt;(SELECT t1.gid, IFNULL(t1.sum1, 0) AS sum1, IFNULL(t2.sum2, 0) AS sum2, IFNULL(t3.sum3, 0) AS sum3
  FROM (SELECT sum(f1) AS sum1, gid FROM table1 GROUP BY gid) AS t1
  LEFT JOIN (SELECT sum(f1) AS sum2, gid FROM table2 GROUP BY gid) AS t2 ON (t1.gid = t2.gid)
  LEFT JOIN (SELECT sum(f1) AS sum3, gid FROM table3 GROUP BY gid) AS t3 ON (t1.gid = t3.gid)
) UNION (
SELECT t2.gid, IFNULL(t1.sum1, 0) AS sum1, IFNULL(t2.sum2, 0) AS sum2, IFNULL(t3.sum3, 0) AS sum3
  FROM (SELECT sum(f1) AS sum2, gid FROM table2 GROUP BY gid) AS t2
  LEFT JOIN (SELECT sum(f1) AS sum1, gid FROM table1 GROUP BY gid) AS t1 ON (t2.gid = t1.gid)
  LEFT JOIN (SELECT sum(f1) AS sum3, gid FROM table3 GROUP BY gid) AS t3 ON (t2.gid = t3.gid)
) UNION (
SELECT t3.gid, IFNULL(t1.sum1, 0) AS sum1, IFNULL(t2.sum2, 0) AS sum2, IFNULL(t3.sum3, 0) AS sum3
  FROM (SELECT sum(f1) AS sum3, gid FROM table3 GROUP BY gid) AS t3
  LEFT JOIN (SELECT sum(f1) AS sum1, gid FROM table1 GROUP BY gid) AS t1 ON (t3.gid = t1.gid)
  LEFT JOIN (SELECT sum(f1) AS sum2, gid FROM table2 GROUP BY gid) AS t2 ON (t3.gid = t2.gid)
) ORDER BY gid;
+------+------+------+------+
&#124; gid  &#124; sum1 &#124; sum2 &#124; sum3 &#124;
+------+------+------+------+
&#124;    1 &#124;   12 &#124;    2 &#124;    0 &#124;
&#124;    2 &#124;    0 &#124;   13 &#124;    9 &#124;
&#124;    3 &#124;   10 &#124;   11 &#124;   21 &#124;
&#124;    4 &#124;   48 &#124;   14 &#124;    0 &#124;
&#124;    5 &#124;    0 &#124;   37 &#124;   33 &#124;
&#124;    6 &#124;   22 &#124;   23 &#124;   45 &#124;
+------+------+------+------+&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>For anyone curious, method #1 CAN be extended to handle more than 2 tables&#8230; It just gets ugly.  Basically, for each table LEFT JOIN all other tables to it,  Then union each of those together.</p>
<pre>CREATE TABLE `table1` (
  `f1` int(11) default NULL,
  `gid` int(11) default NULL
) ENGINE=MyISAM;

CREATE TABLE `table2`...

CREATE TABLE `table3`...

INSERT INTO table1 VALUES(1,1), (4,1), (7,1), (10,3), (13,4), (16,4), (19,4), (22,6);
INSERT INTO table2 VALUES(2,1), (5,2), (8,2), (11,3), (14,4), (17,5), (20,5), (23,6);
INSERT INTO table3 VALUES(3,2), (6,2), (9,3), (12,3), (15,5), (18,5), (21,6), (24,6);</pre>
<p>Here I&#8217;m trying to get the SUM of each group of numbers within a single table and display them together in the same result so I need a subquery here&#8230; i.e. table1, group 1 is 1+4+7 = 12; table3, group 2 is 3+6 = 9 &#8230;.</p>
<pre>(SELECT t1.gid, IFNULL(t1.sum1, 0) AS sum1, IFNULL(t2.sum2, 0) AS sum2, IFNULL(t3.sum3, 0) AS sum3
  FROM (SELECT sum(f1) AS sum1, gid FROM table1 GROUP BY gid) AS t1
  LEFT JOIN (SELECT sum(f1) AS sum2, gid FROM table2 GROUP BY gid) AS t2 ON (t1.gid = t2.gid)
  LEFT JOIN (SELECT sum(f1) AS sum3, gid FROM table3 GROUP BY gid) AS t3 ON (t1.gid = t3.gid)
) UNION (
SELECT t2.gid, IFNULL(t1.sum1, 0) AS sum1, IFNULL(t2.sum2, 0) AS sum2, IFNULL(t3.sum3, 0) AS sum3
  FROM (SELECT sum(f1) AS sum2, gid FROM table2 GROUP BY gid) AS t2
  LEFT JOIN (SELECT sum(f1) AS sum1, gid FROM table1 GROUP BY gid) AS t1 ON (t2.gid = t1.gid)
  LEFT JOIN (SELECT sum(f1) AS sum3, gid FROM table3 GROUP BY gid) AS t3 ON (t2.gid = t3.gid)
) UNION (
SELECT t3.gid, IFNULL(t1.sum1, 0) AS sum1, IFNULL(t2.sum2, 0) AS sum2, IFNULL(t3.sum3, 0) AS sum3
  FROM (SELECT sum(f1) AS sum3, gid FROM table3 GROUP BY gid) AS t3
  LEFT JOIN (SELECT sum(f1) AS sum1, gid FROM table1 GROUP BY gid) AS t1 ON (t3.gid = t1.gid)
  LEFT JOIN (SELECT sum(f1) AS sum2, gid FROM table2 GROUP BY gid) AS t2 ON (t3.gid = t2.gid)
) ORDER BY gid;
+------+------+------+------+
| gid  | sum1 | sum2 | sum3 |
+------+------+------+------+
|    1 |   12 |    2 |    0 |
|    2 |    0 |   13 |    9 |
|    3 |   10 |   11 |   21 |
|    4 |   48 |   14 |    0 |
|    5 |    0 |   37 |   33 |
|    6 |   22 |   23 |   45 |
+------+------+------+------+</pre>
]]></content:encoded>
	</item>
</channel>
</rss>
