gpt4 book ai didi

mysql - 如何组合从数据库派生的两个查询

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

我有两个问题

$sql1="SELECT MDN, AVG(RSRP),count(case when RSRP !=0 THEN RSRP END) as total from CPE where (month(LAST_UPDATED_TIME)=1) group by MDN";
$sql2="SELECT MDN, AVG(RSRP),count(case when RSRP !=0 THEN RSRP END) as total from CPE where (month(LAST_UPDATED_TIME)=2) group by MDN";

我的问题是如何将这两个查询合并到一个有 6 列的表中

我试过这段代码,但它给出了错误

select
(SELECT MDN, AVG(RSRP),count(case when RSRP !=0 THEN RSRP END) as total from CPE where (month(LAST_UPDATED_TIME)=1) group by MDN) as 1,
(SELECT MDN, AVG(RSRP),count(case when RSRP !=0 THEN RSRP END) as total from CPE where (month(LAST_UPDATED_TIME)=2) group by MDN) as 2

请帮我解决这个问题..

最佳答案

你可以加入

  SELECT t1.MDN
, AVG(t1.RSRP)
, count(case when t1.RSRP !=0 THEN t1.RSRP END) as total
, t2.avg2
, t2.total2
from CPE t1
LEFT JOIN(
SELECT MDN
, AVG(RSRP) avg2
,count(case when RSRP !=0 THEN RSRP END) as total2
from CPE
where (month(LAST_UPDATED_TIME)=2)
group by MDN
) t2 on t1.MDN = t2.MDN
where (month(t1.LAST_UPDATED_TIME)=1)
group by t1.MDN

关于mysql - 如何组合从数据库派生的两个查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49168345/

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