I have a Users table and a Payments table. I need a query which lists the users who have a record in the payments table where PaymentStatus=1.
It must also only list users with a certain GroupID. The GroupID will be passed by a variable, but we can just call it 5 for simplicity.
The timestamp of the record (TimeCompleted) must also be less than a set timestamp, but we can just call this 10.
These are the columns in the tables (simplified):
Users: UserID, GroupID, UserName
Payments: PaymentID, UserID, PaymentStatus, TimeCompleted
The query should select the field UserName.
I did ask a similar question a few hours ago, but I asked it wrong and I couldn't get the queries to work properly for me. Also - if the user has more than one valid and active payment, it must only return the UserName once. When I tried to do it, I couldn't do this.
You can improve on my current query if you want to (it does everything apart from the last problem about duplicating the returned users). I might be going about it completely wrong though:
SELECT `Users`.`UserName`
FROM `Users`
LEFT OUTER JOIN `Payments` ON `Users`.`UserID` = `Payments`.`UserID`
WHERE `Payments`.`TimeCompleted` < 10
AND `Payments`.`PaymentStatus` = 1
AND `Users`.`GroupID` = 5