[英]mysql combine two queries into one query
I have two working queries. 我有两个工作查询。 The first one selects the observed temperatures of a given month.
第一个选择给定月份的观测温度。 The second selects the normal average for each day of that month.
第二个选择该月每一天的正常平均值。
SELECT data, Tmax, Tmin, Mitjana, Pluja
FROM $estacio
WHERE data BETWEEN '2014-03-01' AND '2014-03-31'
SELECT data, round(avg(Mitjana),1)
FROM $estacio
WHERE month(data)='03'
group by day(data), month(data)
order by data
I'm trying to find the difference between the observed avg and the normal avg and insert into the first query as a new column. 我试图找到观察到的平均值与普通平均值之间的差异,并将其作为新列插入第一个查询中。 (Mitjana is mean)
(Mitjana是卑鄙的)
Simply added the second query to the first one. 只需将第二个查询添加到第一个查询即可。
SELECT data, Tmax, Tmin, Mitjana, Pluja, (
(SELECT NormalAvg FROM
(SELECT data, round(avg(Mitjana),1) AS NormalAvg FROM $estacio
WHERE month(data)='03'
group by day(data), month(data) order by data) AS NormalAvgList
WHERE NormalAvgList.data=est.data LIMIT 1)-Mitjana) AS Diff
FROM $estacio AS est WHERE data BETWEEN '2014-03-01' AND '2014-03-31'
Tested with SQL Fiddle . 经过SQL Fiddle测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.