SELECT accounts which logged in at least once per month since registering with MySQL
07:08 09 Jul 2014

I am trying to figure out retained users for our application using the login history we keep for every user.

I want to know users who registered in month 1, then came back in month 2, then also came back in month 3 and so on.....

A single query is simple:

Query 1

SELECT DISTINCT player_id 
FROM login_history 
WHERE timestamp BETWEEN UNIX_TIMESTAMP('2014-01-01 00:00:00') 
AND (UNIX_TIMESTAMP('2014-02-01 00:00:00')-1);

The above gives me unique logins for January, simple.

Query 2

SELECT DISTINCT player_id 
FROM login_history 
WHERE timestamp BETWEEN UNIX_TIMESTAMP('2014-02-01 00:00:00') 
AND (UNIX_TIMESTAMP('2014-03-01 00:00:00')-1);

The above now gives me the unique logins for February.

How can I join the two, or more, to give me the player_id's common to all results?

I have tried using IN statements but that is hugely slow.

php mysql datetime