Mysql 在何处加入MAX

Mysql 在何处加入MAX,mysql,sql,database,join,Mysql,Sql,Database,Join,共有两个表格: 现在的表温度: 表1温度历史记录: 温度\u now.machine\u id=温度\u history.machine\u id 更改\u id为自动增量 我需要得到每台机器的最后一次温度变化: 谢谢您的帮助。您可以使用下面的通用表表达式来完成 select n.machine_id, n.temperature as temperature_now, h.temperature as temperature_before, h.controller from tem

共有两个表格:

现在的表温度:

表1温度历史记录:

温度\u now.machine\u id=温度\u history.machine\u id

更改\u id为自动增量

我需要得到每台机器的最后一次温度变化:


谢谢您的帮助。

您可以使用下面的通用表表达式来完成

select n.machine_id, n.temperature as temperature_now, 
    h.temperature as temperature_before, h.controller
from temperature_now n
left outer join (
    select th.machine_id, max(th.change_id) as MaxChangeID
    from temperature_history th
    Inner join temperature_now tn on th.machine_id = tn.machine_id and th.temperature <> tn.temperature
    group by th.machine_id
) hm on n.machine_id = hm.machine_id
left outer join temperature_history h on hm.machine_id = h.machine_id
    and hm.MaxChangeID = h.change_id
order by n.machine_id
更新

然后您将得到如下结果:

machine_id   temperature    temperature_before  Controller
1            15             7                   John
2            20             9                   Steve

这可能有效,但嵌套查询可能存在性能问题。 如果我可以优化查询,我会发布它


希望对您有所帮助

@Koralek M.您是否有日期的概念,或者您将直接与rowid合作?罗维德直接。。。不是一个好的做法您需要像date列或其他列(如Iscurrent)这样的列来跟踪更改,因为使用id不是一个好方法practice@JosvicZammit有一列写着变更日期,但有可能,该日期不会是唯一的,所以我使用IDB,但我需要获取更改的数据此查询是在SQL Server 2008中编写的,我需要获取上次更改的温度。无论温度是否改变,代码都将返回上一个温度
machine_id | temperature_now | temperature_before | controller
--------------------------------------------------------------
    1              15                   7              John
    2              20                   9              Steve
select n.machine_id, n.temperature as temperature_now, 
    h.temperature as temperature_before, h.controller
from temperature_now n
left outer join (
    select th.machine_id, max(th.change_id) as MaxChangeID
    from temperature_history th
    Inner join temperature_now tn on th.machine_id = tn.machine_id and th.temperature <> tn.temperature
    group by th.machine_id
) hm on n.machine_id = hm.machine_id
left outer join temperature_history h on hm.machine_id = h.machine_id
    and hm.MaxChangeID = h.change_id
order by n.machine_id
WITH History (MachineID, Temp, Controller)
AS
(
   SELECT machine_id, temperature, controller 
   FROM dbo.temperature_history
   WHERE change_id in ((SELECT MAX(change_id) FROM dbo.temperature_history AS TH
   INNER JOIN dbo.temperature_now AS TN
   ON TN.machine_id = TH.machine_id
   WHERE TN.temperature != TH.temperature 
   GROUP BY TH.machine_id))
)

SELECT N.machine_id, 
N.temperature, 
H.Temp as 'temperature_before',
H.Controller
FROM dbo.temperature_now AS N
INNER JOIN History AS H
ON H.MachineID = N.machine_id
ORDER BY N.machine_id;
machine_id   temperature    temperature_before  Controller
1            15             7                   John
2            20             9                   Steve

SELECT DISTINCT TN.MACHINE_ID,
                TN.TEMPERATURE,
                (SELECT DISTINCT TH.TEMPERATURE
                   FROM TEMPERATURE_HISTORY TH
                  WHERE TH.CHANGE_ID =
                        (SELECT MAX(TH1.CHANGE_ID)
                           FROM TEMPERATURE_HISTORY TH1
                          WHERE TH1.MACHINE_ID = TN.MACHINE_ID
                            AND TH1.CHANGE_ID <
                                (SELECT MAX(TH2.CHANGE_ID)
                                   FROM TEMPERATURE_HISTORY TH2
                                  WHERE TH2.MACHINE_ID = TN.MACHINE_ID))) AS TEMPERATURE_BEFORE,
                (SELECT DISTINCT TH.CONTROLLER
                   FROM TEMPERATURE_HISTORY TH
                  WHERE TH.CHANGE_ID =
                        (SELECT MAX(TH1.CHANGE_ID)
                           FROM TEMPERATURE_HISTORY TH1
                          WHERE TH1.MACHINE_ID = TN.MACHINE_ID
                            AND TH1.CHANGE_ID <
                                (SELECT MAX(TH2.CHANGE_ID)
                                   FROM TEMPERATURE_HISTORY TH2
                                  WHERE TH2.MACHINE_ID = TN.MACHINE_ID))) AS CONTROLLER
  FROM TEMPERATURE_NOW TN