The following structure is how to envision this query.
user_info
- id int(11)
- email varchar(255)
- regdate int(11)
statistics
- id int(11)
- playerid int(11)
- timetopay int(11)
All I want to do is get all unique emails from user_info then get the average of statistics.timetopay-user_info.regdate but in the same query.
SELECT AVG(s.timetopay-ui.regdate) as time_amount FROM user_info ui
LEFT JOIN statistics s ON ui.id=s.playerid
WHERE s.timetopay>'0' GROUP BY ui.email ORDER BY ui.id DESC LIMIT 1
Hopefully from the above snippet you’ll get the gist. I’m just trying to work out the average time it takes a user to pay some money but only if they have.
The above query does not quite work for me at the moment.
I’m having to use group by ui.email as users can have more than one account. Anyone know how I can achieve this in one query?
Is this what you’re looking for?
Note if the user hasn’t made in purchase it will not be displayed in the results. If you want it to then change it to a left join and add a
coalesceto default to whatever value you want:Additionally, I wouldn’t recommend to use ints as dates… there is a Date data type you should be using.