gpt4 book ai didi

mysql - 如何连接这两个mysql查询?

转载 作者:行者123 更新时间:2023-11-29 14:06:57 24 4
gpt4 key购买 nike

我希望能够加入两种复杂的查询,但不太确定执行此操作的语法。

查询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);

给出了这个结果:

enter image description here

查询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;

给出了这个结果:

enter image description here

我想加入这 2 个查询,以便当 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;

本质上,连接当月的两个临时表(作为查询结果形成)。

关于mysql - 如何连接这两个mysql查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14198954/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com