Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何加入这两个mysql查询?_Mysql_Join - Fatal编程技术网

如何加入这两个mysql查询?

如何加入这两个mysql查询?,mysql,join,Mysql,Join,我有两种复杂的查询,我希望能够加入,但不太确定语法 问题1: select month(Date_1) as Date_1, sum(number_1) as number_1 from table_1 where Date_1 between '2012-01-01' and '2013-01-01' group by month(Date_1); 结果如下: 问题2: select count(distinct number_2) as number_2

我有两种复杂的查询,我希望能够加入,但不太确定语法

问题1:

select month(Date_1) as Date_1, 
       sum(number_1) as number_1 
  from table_1 
  where Date_1 between '2012-01-01' and '2013-01-01' 
  group by month(Date_1);
结果如下:

问题2:

select count(distinct number_2) as number_2, 
       month(Date_2) as Date_2 
  from table_2 
  where Date_2 between '2012-01-01' and '2013-01-01' 
  group by month(Date_2) 
  order by month(Date_2) desc;
结果如下:

我想加入这两个查询,以便在Date_1和Date_2列相等时,将number_1与number_2匹配。每月一排

有人知道怎么做吗

select T1.Date_1, T1.number_1, T2.number_2 from 

(select month(Date_1) as Date_1, 
       sum(number_1) as number_1 
  from table_1 
  where Date_1 between '2012-01-01' and '2013-01-01' 
  group by month(Date_1))T1

inner join

(select count(distinct number_2) as number_2, 
       month(Date_2) as Date_2 
  from table_2 
  where Date_2 between '2012-01-01' and '2013-01-01' 
  group by month(Date_2) 
  order by month(Date_2) desc)T2

on T1.Date_1 = T2.Date_2;

本质上,将当月的两个临时表(通过查询形成)合并起来。

您的意思是要将两个数字以及它们的匹配日期(如1 964789.36 137)放在一行中吗?是的,这就是我想要的,下面的答案解决了问题。