MySQL UNION ALL 与 LEFT JOIN 返回双精度值
MySQL UNION ALL with LEFT JOIN returning double values
我有多个表试图在 MySql 中使用 UNION ALL 和 LEFT JOIN 从所有表中获取值,我使用的查询如下所示。
(SELECT
t1.name, t2.cat, t3.utt, t4.gcost, t4.net
FROM table_one as t1
LEFT JOIN table_three as t2
ON t2.id = t1.ct
LEFT JOIN table_four as t3
ON t3.id = t1.ut
LEFT JOIN table_five as t4
ON t4.gid = t1.id
) UNION ALL
(SELECT
t1.name, t2.cat, t3.utt, t4.gcost, t4.net
FROM table_tow as t1
LEFT JOIN table_three as t2
ON t2.id = t1.ct
LEFT JOIN table_four as t3
ON t3.id = t1.ut
LEFT JOIN table_five as t4
ON t4.gid = t1.id
)
但是上面的查询 return double values in MySQL
这里是 fiddle link: http://sqlfiddle.com/#!9/bd30ba/1
如有任何帮助,我们将不胜感激。
谢谢
更新
根据 P.Salmon 问题了解上述查询用于固定资产应用程序的完整逻辑,因此固定资产具有多个项目值,例如:
- 实际价格。
- 累计折旧
- 当前值
- ...
所以 fiddle 中的查询有 2 个不同的 price/amount 查询应该 return 每个引用行 ID 的两个值。
您只想要一个并且真的不在乎哪个然后最大化 t5 详细信息
select t1.name,t3.cat,t4.utt,
t5.gcost,t5.net
from
(
select * from table_one
union all
select * from table_two) t1
LEFT JOIN table_three as t3 ON t3.id = t1.ct
LEFT JOIN table_four as t4 ON t4.id = t1.ct
left join (select gid,max(gcost) gcost,max(net) net from table_five group by gid) t5 on t5.gid = t1.id;
+--------------+------+------+-------+------+
| name | cat | utt | gcost | net |
+--------------+------+------+-------+------+
| Green Apple | A | AAA | 3000 | 6542 |
| Small Orange | B | BBB | 2560 | 6542 |
| Small Banana | C | CCC | 5800 | 6542 |
| Small Mango | C | DDD | 2560 | 6542 |
| Red Apple | A | AAA | 3000 | 6542 |
| Big Orange | B | BBB | 2560 | 6542 |
| Big Banana | C | CCC | 5800 | 6542 |
| Big Mango | C | DDD | 2560 | 6542 |
+--------------+------+------+-------+------+
8 rows in set (0.072 sec)
谢谢大家的帮助,但其中 none 帮助了我,我知道 table_five 中有重复的 ID,来自 table_one 和 table_tow...我解决问题的唯一方法是在 table_one、table_tow ... AS
中添加新的序列 ID
seq_.time();
相同的序列在插入过程中添加 table_five.. 之后我将查询更改为.
(SELECT
t1.name, t2.cat, t3.utt, t4.gcost, t4.net
FROM table_one as t1
LEFT JOIN table_three as t2
ON t2.id = t1.ct
LEFT JOIN table_four as t3
ON t3.id = t1.ut
LEFT JOIN table_five as t4
ON t4.gid = t1.id
AND t4.seq = t1.seq
) UNION ALL
(SELECT
t1.name, t2.cat, t3.utt, t4.gcost, t4.net
FROM table_tow as t1
LEFT JOIN table_three as t2
ON t2.id = t1.ct
LEFT JOIN table_four as t3
ON t3.id = t1.ut
LEFT JOIN table_five as t4
ON t4.gid = t1.id
AND t4.seq = t1.seq
)
现在我得到了我想要的准确数据。
谢谢
我有多个表试图在 MySql 中使用 UNION ALL 和 LEFT JOIN 从所有表中获取值,我使用的查询如下所示。
(SELECT
t1.name, t2.cat, t3.utt, t4.gcost, t4.net
FROM table_one as t1
LEFT JOIN table_three as t2
ON t2.id = t1.ct
LEFT JOIN table_four as t3
ON t3.id = t1.ut
LEFT JOIN table_five as t4
ON t4.gid = t1.id
) UNION ALL
(SELECT
t1.name, t2.cat, t3.utt, t4.gcost, t4.net
FROM table_tow as t1
LEFT JOIN table_three as t2
ON t2.id = t1.ct
LEFT JOIN table_four as t3
ON t3.id = t1.ut
LEFT JOIN table_five as t4
ON t4.gid = t1.id
)
但是上面的查询 return double values in MySQL
这里是 fiddle link: http://sqlfiddle.com/#!9/bd30ba/1
如有任何帮助,我们将不胜感激。
谢谢
更新
根据 P.Salmon 问题了解上述查询用于固定资产应用程序的完整逻辑,因此固定资产具有多个项目值,例如:
- 实际价格。
- 累计折旧
- 当前值
- ...
所以 fiddle 中的查询有 2 个不同的 price/amount 查询应该 return 每个引用行 ID 的两个值。
您只想要一个并且真的不在乎哪个然后最大化 t5 详细信息
select t1.name,t3.cat,t4.utt,
t5.gcost,t5.net
from
(
select * from table_one
union all
select * from table_two) t1
LEFT JOIN table_three as t3 ON t3.id = t1.ct
LEFT JOIN table_four as t4 ON t4.id = t1.ct
left join (select gid,max(gcost) gcost,max(net) net from table_five group by gid) t5 on t5.gid = t1.id;
+--------------+------+------+-------+------+
| name | cat | utt | gcost | net |
+--------------+------+------+-------+------+
| Green Apple | A | AAA | 3000 | 6542 |
| Small Orange | B | BBB | 2560 | 6542 |
| Small Banana | C | CCC | 5800 | 6542 |
| Small Mango | C | DDD | 2560 | 6542 |
| Red Apple | A | AAA | 3000 | 6542 |
| Big Orange | B | BBB | 2560 | 6542 |
| Big Banana | C | CCC | 5800 | 6542 |
| Big Mango | C | DDD | 2560 | 6542 |
+--------------+------+------+-------+------+
8 rows in set (0.072 sec)
谢谢大家的帮助,但其中 none 帮助了我,我知道 table_five 中有重复的 ID,来自 table_one 和 table_tow...我解决问题的唯一方法是在 table_one、table_tow ... AS
中添加新的序列 IDseq_.time();
相同的序列在插入过程中添加 table_five.. 之后我将查询更改为.
(SELECT
t1.name, t2.cat, t3.utt, t4.gcost, t4.net
FROM table_one as t1
LEFT JOIN table_three as t2
ON t2.id = t1.ct
LEFT JOIN table_four as t3
ON t3.id = t1.ut
LEFT JOIN table_five as t4
ON t4.gid = t1.id
AND t4.seq = t1.seq
) UNION ALL
(SELECT
t1.name, t2.cat, t3.utt, t4.gcost, t4.net
FROM table_tow as t1
LEFT JOIN table_three as t2
ON t2.id = t1.ct
LEFT JOIN table_four as t3
ON t3.id = t1.ut
LEFT JOIN table_five as t4
ON t4.gid = t1.id
AND t4.seq = t1.seq
)
现在我得到了我想要的准确数据。 谢谢