I am trying to do an INNER JOIN on two queries. If I have a broad SELECT statement for the first component, it works fine. If I try to add a WHERE... to the first component, I get an uninformative Syntax error and I don't know why.
This is what works but is slow because my table is big (~1mil rows)
SELECT a.RoomNum, a.EventName, a.EventStatus, a.EventDateTime FROM eventtable a
INNER JOIN
(
SELECT `RoomNum`, eventname, MAX(`EventDateTime`) as MT
FROM `eventtable`
WHERE eventname = "DND"
Group BY RoomNum
) b
ON a.RoomNum = b.RoomNum and a.EventDateTime = b.MT and a.eventname = b.eventname
But what I really need is:
SELECT a.RoomNum, a.EventName, a.EventStatus, a.EventDateTime from eventtable WHERE a.EventName = "DND" a
INNER JOIN
(
SELECT `RoomNum`, eventname, MAX(`EventDateTime`) as MT
FROM `eventtable`
where eventname = "DND"
Group by RoomNum
) b
on a.RoomNum = b.RoomNum and a.EventDateTime = b.MT and a.eventname = b.eventname
I thought of assigning result to a table but didn't really want to have to generate a table and then drop it after to make sure next time the query is made, there will be no table already present.
Thanks.