sql join查询数据库,没有想要的结果,

时间:2018-05-15 09:40:55

标签: mysql sql

我的要求是:从数据库查询最新数据,按时间字段作为标准,查询报警时间这个字段,但是报警时间<时间 执行sql:

SELECT
    c.entryTime AS alarmtime ,
    a.time AS time
FROM (
    SELECT
        t.DB33,
        MAX( t.Time ) AS time,
        t.Stream,
        t.Coil,
        t.`View` 
    FROM ( 
        SELECT DB33, Time, Stream, Coil, `View` 
        FROM running_check 
        ORDER BY id DESC 
        LIMIT 1000 ) as t 
    GROUP BY t.DB33 ) AS a
LEFT JOIN (
    select cameraID,cameraName 
    from monitor_link_info) as b ON a.db33 = b.cameraID
LEFT JOIN ( 
    SELECT entryTime, cameraID 
    FROM result_video_v2 
    ORDER BY id DESC 
    limit 1000 ) AS c ON a.db33 = c.cameraId 
GROUP BY a.db33, b.cameraName 
ORDER BY a.time DESC, alarmtime DESC

执行结果不可靠,请看图像 现在时间列不是我想要的,因为他没有改变。

enter image description here

1 个答案:

答案 0 :(得分:0)

SELECT
    c.entryTime AS alarmtime ,
    a.time AS TIME

    FROM 
    ( 
        SELECT DB33, MAX(TIME) TIME, Stream, Coil, `View` 
        FROM running_check 
        ORDER BY id DESC 
        GROUP BY t.DB33
        LIMIT 1000 
    ) AS a 

    LEFT JOIN 
    (
        SELECT cameraID,cameraName 
        FROM monitor_link_info
        GROUP BY cameraID
        LIMIT 1000
    ) AS b ON (a.db33 = b.cameraID)
    LEFT JOIN ( 
        SELECT entryTime, cameraID 
        FROM result_video_v2 
        GROUP BY cameraID
        ORDER BY id DESC
        LIMIT 1000
    ) AS c ON (a.db33 = c.cameraID) 

GROUP BY a.db33, b.cameraName 
ORDER BY a.time DESC, c.entryTime DESC