简体   繁体   English

mysql中的完整外部联接或联合

[英]Full OUTER JOIN OR UNION in mysql

I've two temporary table say temp1 and temp2(which are created in runtime). 我有两个临时表,分别是temp1和temp2(在运行时创建)。 I need to perform "FULL outer JOIN" to get the data from both the table. 我需要执行“ FULL external JOIN”以从两个表中获取数据。 But I got error 但是我有错误

Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your 
MySQL server version for the right syntax to use near 'FULL OUTER JOIN cashExtTemp t2 
ON t1.code = t2.code'

SELECT * FROM cashIntTemp t1 FULL OUTER JOIN cashExtTemp t2 ON t1.code = t2.code

And came to know that FULL OUTER JOIN is NOT possible in MySQL from this link Full Outer Join in MySQL and tried to implement UNION as given in the link. 并且从该链接知道MySQL中无法进行FULL OUTER JOIN 在MySQL中尝试了Full Outer Join,并尝试实现该链接中给出的UNION。 Since I'm using temporary table it doesn't work and i got the error as 由于我正在使用临时表,因此无法正常工作,并且出现如下错误

Error Number: 1137 
Can't reopen table: 't1'

This is my UNION Query 这是我的UNION查询

SELECT * FROM cashIntTemp t1 LEFT JOIN cashExtTemp t2 ON t1.code = t2.code
UNION
SELECT * FROM cashIntTemp t1 RIGHT JOIN cashExtTemp t2 ON t1.code = t2.code

The data in the table would be like this TABLE: cashIntTemp 该表中的数据将如下表所示:cashIntTemp

code     qty     date
P001     100    2013-11-29 
P003     200    2013-11-30
P005     600    2013-11-30

The data in the table would be like this TABLE: cashIntTemp 该表中的数据将如下表所示:cashIntTemp

code     qty     date
P001     110    2013-11-29 
P002     250    2013-12-01
P005     650    2013-12-01

I need a query to get all the data from both the tables. 我需要查询以从两个表中获取所有数据。

I need the result in this format 我需要这种格式的结果

code     qty     date        code    qty     date
P001     100    2013-11-29   P001    110   2013-11-29
P002     250    2013-12-01
P003     200    2013-11-29   
P005     600    2013-11-29   P005    650   2013-11-29

So please help me on this. 所以请帮我。 Thanks in advance. 提前致谢。

edit: totally missed the part about temporary tables, don't know if this will work... 编辑:完全错过了有关临时表的部分,不知道这是否有效...

sqlfiddle: http://sqlfiddle.com/#!2/3bf61/10 sqlfiddle: http ://sqlfiddle.com/#!2/3bf61/10

I'm not getting 100% of what you're throwing down, let me try to rephrase: You have 2 tables: cashIntTemp and cashExtTemp . 我没有得到您要扔掉的东西的100%,让我尝试改写一下:您有2个表: cashIntTempcashExtTemp In a single query, you want to fetch all rows from both tables. 在单个查询中,您要从两个表中获取所有行。 The intended result is: 预期的结果是:

CODE    QTY DATE
P001    100 November, 29 2013 00:00:00+0000
P002    250 December, 01 2013 00:00:00+0000
P003    200 November, 30 2013 00:00:00+0000
P005    650 December, 01 2013 00:00:00+0000
P005    600 November, 30 2013 00:00:00+0000

A UNION will provide what you want, again per @Ryan, as long as they are different tables. 只要它们是不同的表, UNION将再次为每个@Ryan提供所需的内容。 Part of your question references two different tables, while the other part references the same table. 您的问题的一部分引用了两个不同的表,而另一部分引用了相同的表。

Shouldn't the following be what you're after? 以下不是您所追求的吗?

SELECT * FROM `cashIntTemp` as `t1`
UNION
SELECT * FROM `cashExtTemp` as `t2`
ORDER BY `code`

Or, if you don't want to filter duplicate rows, use UNION ALL : http://sqlfiddle.com/#!2/3bf61/11 或者,如果您不想过滤重复的行,请使用UNION ALLhttp : UNION ALL /11

Query: 查询:

SELECT * FROM `cashIntTemp` as `t1`
UNION ALL
SELECT * FROM `cashExtTemp` as `t2`
ORDER BY `code`

Result: 结果:

CODE    QTY DATE
P001    100 November, 29 2013 00:00:00+0000
P001    100 November, 29 2013 00:00:00+0000
P002    250 December, 01 2013 00:00:00+0000
P003    200 November, 30 2013 00:00:00+0000
P005    650 December, 01 2013 00:00:00+0000
P005    600 November, 30 2013 00:00:00+0000

In MySql, you can't access a single temp table multiple times in the same query. 在MySql中,您不能在同一查询中多次访问单个临时表。 You're emulating full outer join with a left join unioned to a right join, correct? 您正在模拟将左联接与右联接结合在一起的完全外部联接,对吗?

You'll have to insert your result set for the left join and your result set for the right join in to a separate temp table in separate queries (ie, not using a union). 您必须将左侧联接的结果集和右侧联接的结果集插入到单独的查询中的单独的临时表中(即,不使用并集)。

Make sense? 说得通?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM