排序依据和分组依据不起作用?

时间:2019-02-06 10:57:09

标签: mysql

我有以下声明

SELECT
verification_booking.id, verification_booking.reg, verification_booking.status,
verification_updates.ver_id, verification_updates.notes, verification_updates.systime, verification_updates.sysdate,
verification_updates.event
FROM verification_updates
INNER JOIN verification_booking 
ON verification_updates.ver_id = verification_booking.id
WHERE verification_booking.status = 'Open' AND DATE(verification_updates.sysdate) = CURDATE() 
GROUP BY verification_updates.ver_id
ORDER BY verification_updates.systime DESC  

我试图获取按时间段顺序输入的最后一条记录,但是出于某种原因却给了我第一条记录?是否存在内部联接问题或我的说法有误

1 个答案:

答案 0 :(得分:0)

尝试一下

SELECT 
    verification_booking.id,
    verification_booking.reg,
    verification_booking.status,
    verification_updates.ver_id,
    verification_updates.notes,
    verification_updates.systime,
    verification_updates.sysdate,
    verification_updates.event
FROM
    verification_updates
        INNER JOIN
    verification_booking
WHERE
    verification_updates.systime IN (SELECT 
            MAX(systime)
        FROM
            verification_updates
                INNER JOIN
            verification_booking ON verification_updates.ver_id = verification_booking.id
        WHERE
            verification_booking.status = 'Open'
                AND DATE(verification_updates.sysdate) = CURDATE()
        GROUP BY verification_updates.ver_id)
ORDER BY verification_updates.systime DESC;