简体   繁体   English

查询以获取两个表中的最大值mysql

[英]query to get max value in two tables mysql

I need to get the max value in two tables and display in four columns differents. 我需要在两个表中获取最大值并以四列差异显示。 Celsius | 摄氏| Hum | 嗡嗡声 fecha_temp | fecha_temp | fecha_hum fecha_hum

Table: temperaturas
Columns:
 id_temp int(11) AI PK 
 celsius_temp decimal(10,0) 
 fah_temp decimal(10,0) 
 fecha_temp datetime

Table: humedad
Columns:
 id_hum int(11) AI PK 
 hum_hum float 
 fecha_hum datetime

I tried this query but doesn't work 我尝试了此查询,但不起作用

select t.celsius_temp as celsius, h.hum_hum, t.fecha_temp, h.fecha_hum from 
temperaturas t
inner join humedad h on h.hum_hum <=(select max(h.hum_hum) from humedad h)
where t.celsius_temp<=(select max(t.celsius_temp) from temperaturas t) and 
t.fecha_temp between '2017-12-01' and '2017-12-05'
order by t.celsius_temp, h.hum_hum desc limit 1;

Thanks so much 非常感谢

The max value of each table in the period is: 该期间内每个表的最大值为:

SET @start_date = '2017-12-01';
SET @end_date = '2017-12-01';
SELECT MAX(t.celsius_temp) INTO @tmax FROM temperaturas t 
  WHERE t.fecha_temp BETWEEN @start_date AND @end_date;
SELECT MAX(h.hum_hum) INTO @hmax FROM humedad h 
  WHERE t.fecha_hum between @start_date AND @end_date;

You could get them both into one table doing: 您可以将它们都放入一个表中,以执行以下操作:

SELECT @tmax, @hmax;

If you want to know the dates where the max temperature or max humidity is reached it's a bit trickier as you may have multiple dates with the same values. 如果您想知道达到最高温度或最高湿度的日期,那会有些棘手,因为您可能有多个具有相同值的日期。 You could write this as a single query but I'd rather use the above queries and then run: 您可以将其编写为单个查询,但我宁愿使用上述查询,然后运行:

SELECT * from temperaturas t where t.celsius_temp = @maxt;
SELECT * from humedad h where h.hum_hum = @maxh;

Remember, this could have multiple rows if the temperature is the same on multiple dates. 请记住,如果温度在多个日期相同,则可能会有多行。 There is no simple sensible way how would would join this into one table. 没有简单明智的方法将其合并到一个表中。

If you have multiple measurements per day and are looking for the top temperature/humidity on each given day, then you want to use the Group By function. 如果您每天进行多次测量,并且每天都在寻找最高温度/湿度,那么您想使用“分组依据”功能。 You can then join on the date() functio of the timestamp like this: 然后,您可以像这样加入时间戳的date()函数:

SELECT coalesce(date(t.fecha_temp), date(h.fecha_hum)), 
  MAX(t.celsius_temp) as celsius,
  MAX(h.hum_hum) as humibity
FROM temperaturas t 
OUTER JOIN humedad h on date(t.fecha_temp) = date(h.fecha_hum)
WHERE coalesce(date(t.fecha_temp), date(h.fecha_hum)) 
  between @start_date and @end_date
GROUP BY coalesce(date(t.fecha_temp), date(h.fecha_hum))
ORDER BY coalesce(date(t.fecha_temp), date(h.fecha_hum)) 

The coalesce() function here is necessary because you may have dates for which only one table may have data. 这里的coalesce()函数是必需的,因为您可能具有仅一个表可以包含数据的日期。

This join is not efficient if you have a lot of data, in which case you may want to look into creating temporary tables with the group function results and then join just on the single row per date. 如果您有大量数据,则这种联接效率不高,在这种情况下,您可能要考虑使用组函数结果创建临时表,然后每个日期仅在单行上联接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM