我想查 3月2号到 4月6号某个设备名称下,每天的最大值与对应的温度查出来

就算没有这个值,也可以用默认值表示,但是我发现IFNULL 没有效果,还是只会查有值的数据。因为这个时间点的其他设备名称是有值的,所以我得保证每个值的list的长度是相同的,我是这么写的:



SELECT
IFNULL(p_value ,0) AS pValue,
IFNULL(temperature ,0) AS temperature
FROM
t_p_value_table
WHERE
p_value in (
SELECT
MAX(p_value )
FROM
t_p_value_table
WHERE
sys_date  in (select sys_date from t_p_value_table where  sys_date >= '2022-03-02'
 and sys_date < '2022-04-06'  group sys_date )
 and name in (select name from name _relation _table GROUP BY name ) GROUP BY name )



求指导

mysql 查询 如果是空值, 要用 ISNULL函数, ISNULL(exp) 函数的返回值为1 ,空串和有数据都为0

你先单独运行你的这个SQL语句看看有没有把空值查出来

SELECT
MAX(p_value )
FROM
t_p_value_table
WHERE
sys_date  in (select sys_date from t_p_value_table where  sys_date >= '2022-03-02'
 and sys_date < '2022-04-06'  group sys_date )
 and name in (select name from name _relation _table GROUP BY name ) GROUP BY name

select sys_date,
IFNULL(p_value ,0) AS pValue,
IFNULL(temperature ,0) AS temperature
from sys_date
where (sys_date,p_value_max) in

(
select sys_date,max(p_value) as p_value_max from t_p_value_table
where sys_date >= '2022-03-02' and sys_date < '2022-04-06'
group by sys_date
) -- 这是每天和对应的最大p_value

你要注意语句执行的顺序啊一、sql执行顺序 
(1)from 
(3) join 
(2) on 
(4) where 
(5)group by(开始使用select中的别名,后面的语句中都可以使用)
(6) avg,sum.... 
(7)having 
(8) select 
(9) distinct 
(10) order by

时间列表建议用Java生成,List<对象(时间,每天值(int 默认0),温度)>,然后再查询数据库有值的列表,两个list循环,时间相同就赋值

有的 可以查出来

通过IFNULL就可以设置默认值

select *
 from t_p_value_table
 where  sys_date >= '2022-03-02'
 and sys_date <= '2022-04-06 23:59:59'  
and name in (select name from name _relation _table) into temp Temp_Cursor

select name,
         date_format(sys_date ,''%Y-%m-%d'') as sys_date,
         max(p_value)  p_value
 from Temp_Cursor
group by name,date_format(sys_date ,''%Y-%m-%d'')  into temp Temp_Cursor1

select *,
         temperature =(select  max(temperature) 
                                   from Temp_Cursor 
                                 where Temp_Cursor1.name=Temp_Cursor.name 
                                     and Temp_Cursor1.p_value=Temp_Cursor.p_value
                                     and  date_format(Temp_Cursor1.sys_date ,''%Y-%m-%d'')=date_format(Temp_Cursor.sys_date ,''%Y-%m-%d''))
from Temp_Cursor1 

首先你这个查询是错的。你用max当条件的时候,应该在添加上时间条件,防止有时间区间外存在相同的p_value 。
第一种方式建日历表定期把日期导进去,然后查询的时候关联查。(优点查询简单,缺点日历表需要维护)
把日历表添加到这个查询里

select * from (
 select 
 p_value,
 temperature,
 sys_date,
 row_number()over(partition by sys_date order by p_value DESC) r_id
 from t_p_value_table 
 where  sys_date >= '2022-03-02'
 and sys_date < '2022-04-06' and name in (select name from name _relation _table GROUP BY name )
 ) t where t.r_id = 1

第二种方式使用变量输出date然后关联上面的查询,(优点不用新建表,缺点执行效率慢。需要在service层提前计算要查询的天数)


SELECT
    @date := DATE_ADD(@date, INTERVAL + 1 DAY) days
FROM
    (
        SELECT
            @date := DATE_ADD("2022-03-02", INTERVAL - 1 DAY)
        FROM
            t_p_value_table
        LIMIT 30
    ) time

这sql,哎。。。

拿到数据再处理不就行了

我要理解你sql都要花点时间,让我履一下

你把原始数据放出来啊,至少有对应字段吧

把表结构完整发出来,可以帮助分析问题。