我编写了一个查询,允许我使用groupwise max方法获取最近的房间。但是,如何为多列进行groupwise max?
我在下面的查询中,子查询为我提供了最近的房间,然后将它们与主表连接起来。现在我想选择接下来会发生的第一个事件。
Ex:MIN(DATEDIFF(date,now()))> 0
SELECT name, date, t1.Room, descr, t1.D
FROM Events
JOIN
(
SELECT Roomid, Room, Latitude, Longitude,
ACOS(SIN((:lat))*SIN(RADIANS(Latitude)) + COS((:lat))*COS(RADIANS(Latitude))*COS(RADIANS(Longitude)-(:lon)))*(:R) AS D
FROM Rooms
WHERE Latitude>(:minLat) AND Latitude<(:maxLat)
AND Longitude>(:minLon) AND Longitude<(:maxLon)
AND ACOS(SIN((:lat))*SIN(RADIANS(Latitude)) + COS((:lat))*COS(RADIANS(Latitude))*COS(RADIANS(Longitude)-(:lon)))*(:R) < (:rad)
) AS t1 ON Events.roomid = t1.Roomid
GROUP BY Room
ORDER BY D
有人可以给我任何关于如何做到这一点的指示吗?
谢谢!
P.S:该事件应该是下一个发生的事件。我不使用ABS(MIN()),因为这将返回已经发生的事件。
答案 0 :(得分:0)
假设D是您的日期,按日期差异排序并限制1
SELECT name, date, t1.Room, descr, t1.D
FROM Events
JOIN
(
SELECT Roomid, Room, Latitude, Longitude,
ACOS(SIN((:lat))*SIN(RADIANS(Latitude)) + COS((:lat))*COS(RADIANS(Latitude))*COS(RADIANS(Longitude)-(:lon)))*(:R) AS D
FROM Rooms
WHERE Latitude>(:minLat) AND Latitude<(:maxLat)
AND Longitude>(:minLon) AND Longitude<(:maxLon)
AND ACOS(SIN((:lat))*SIN(RADIANS(Latitude)) + COS((:lat))*COS(RADIANS(Latitude))*COS(RADIANS(Longitude)-(:lon)))*(:R) < (:rad)
) AS t1 ON Events.roomid = t1.Roomid
ORDER BY DATEDIFF( D, now())
LIMIT 1