I have about 5 million records in a database table (MySQL 5.6), and I wanted to get the last 2 dates, grouped by ID. Looking around the web, I found examples that allowed me to cobble together the following...
SELECT id, date
FROM
(
SELECT *,
@id_rank := IF(@current_id = id, @id_rank + 1, 1) AS id_rank,
@current_id := id
FROM `data`
ORDER BY id DESC, date DESC
) ranked
WHERE id_rank <= 2
ORDER BY id ASC, date DESC
Running this code from MySQL Workbench, returned 5,700 rows, which is what I expected. I then tried to call this SQL as-is from PHP, using the following SQL string...
$subSql =
"SELECT *, " .
" @id_rank := IF(@current_id = id, @id_rank + 1, 1) AS id_rank, " .
" @current_id := id " .
"FROM `data` " .
"ORDER BY id DESC, date DESC";
$sql =
"SELECT id, date " .
"FROM ($subSql) ranked " .
"WHERE id_rank <= 2 " .
"ORDER BY id ASC, date DESC";
However, running this code resulted in out-of-memory. I them modified the code to return a count of the number of records expected, and instead of the expected 5,700 rows, it returned 4,925,479. So my question is "what do I have to change in my PHP SQL above, to get the correct results that I was getting from MySQL Workbench".