MySQL:是否可以将GROUP-BY的结果加入两个SELECT中? [英] MySQL: Is it possible to JOIN the GROUP-BY'd results to two SELECTs?
本文介绍了MySQL:是否可以将GROUP-BY的结果加入两个SELECT中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个单独的SELECT语句,它们分别是GROUP-BY的,例如:
I have two separate SELECT statements which are both GROUP-BY'd separately e.g.:
SELECT x, y, z FROM a GROUP BY x
SELECT x, n, o FROM b GROUP BY x
我非常想将这两个SELECT结合在一起以合并其列,例如:
I would very much like to JOIN these two SELECTs together to combine their columns, such as:
SELECT x as x1, y, z FROM a GROUP BY x
LEFT JOIN (
SELECT x as x2, n, o FROM b GROUP BY x)
ON x1=x2;
这可能吗?我问是因为MySQL抱怨
Is this possible? I ask because MySQL is complaining
您的SQL语法有错误; 检查对应的手册 您的MySQL服务器版本 在'LEFT JOIN附近使用正确的语法 选择 x作为x2
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 'LEFT JOIN SELECT x as x2
如果可能的话,我对我的语法有什么疑问吗?
If this is possible, any thoughts on what's wrong with my syntax?
非常感谢!
推荐答案
有效
select * from (
(select 1 a,2 b,3 c) t1 left join (select null a,2 b,5 c) t2 on (t1.b=t2.b)
);
或者,
select * from (
(select 1 a,2 b,3 c) t1 left join (select null a,2 b,5 c) t2 using (b)
);
两者都导致
+---+---+---+------+---+---+
| a | b | c | a | b | c |
+---+---+---+------+---+---+
| 1 | 2 | 3 | NULL | 2 | 5 |
+---+---+---+------+---+---+
1 row in set (0.00 sec)
这篇关于MySQL:是否可以将GROUP-BY的结果加入两个SELECT中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文