I am trying to create select statement in procedure.
Here is my code:
SELECT u.id, max(a.time), max(b.time)
FROM buy a, sell b, users u
WHERE a.user_id = u.id AND b.user_id = u.id
GROUP BY u.id;
I need to take the last date between max(a.time) and max(b.time).
Thanks in advance
If your DB supports aliased queries, try this which would be much more efficient:
The inner query could use indexes if they existed to get the max for each column quickly.
While we’re at it, let’s re-code it using the modern join syntax: