I have a friends table with columns user1 and user2 and a users table with a login field.
A friend “pair” can be stored either way, for example user1=Joe and user2=Dave, or it could be user1=Dave and user2=Joe, same thing.
I want to find all friends of a user (say “Joe”) that have been logged in within the last 30 days (I’m not concerned about the date format, just want to be able to do it in 1 query).
Is using a UNION the only solution, does it have the best performance?
A union is not the only option, you can for example do like this:
Whether that is faster than a union depends on several things, mostly on what indexes you have.