mysql按日期时间选择一列中的所有唯一行和另一列中的所有最大行

What I need is to get the most recent (by date_time) unique player_id for each table_id

Table:

buyin_id   player_id   table_id       date_time
---------|-----------|----------|--------------------|
    1    |    10     |    21    | 2015-01-26 00:00:01
    2    |    11     |    21    | 2015-01-26 00:00:02
    3    |    12     |    21    | 2015-01-26 00:00:03
    4    |    10     |    21    | 2015-01-26 00:00:04
    5    |    11     |    21    | 2015-01-26 00:00:05
    6    |    12     |    22    | 2015-01-26 00:00:06
    7    |    13     |    22    | 2015-01-26 00:00:07
    8    |    13     |    22    | 2015-01-26 00:00:08

Desired result:

buyin_id   player_id   table_id       date_time
---------|-----------|----------|--------------------|
    3    |    12     |    21    | 2015-01-26 00:00:03
    4    |    10     |    21    | 2015-01-26 00:00:04
    5    |    11     |    21    | 2015-01-26 00:00:05
    6    |    12     |    22    | 2015-01-26 00:00:06
    8    |    13     |    22    | 2015-01-26 00:00:08

I tried something like this which returns only 1 row instead of 1 row per table_id

SELECT pb.buyin_id, pb.player_id, pb.buyin, pb.cashout, pb.cashout_error, pb.date_time
FROM poker_buyin AS pb
INNER JOIN (SELECT player_id, MAX(date_time) AS MaxDateTime
FROM poker_buyin GROUP BY player_id) groupedpb 
ON pb.player_id = groupedpb.player_id 
AND pb.date_time = groupedpb.MaxDateTime
WHERE pb.player_id = '$player_id'";

The query you've mentioned finds the most recent record for each player id. And then you filter it to find just one player, so you get one row.

If you want to find the most recent record for each player and table, your inner query needs to be this:

            SELECT  MAX(buyin_id) buyin_id
              FROM  poker_buyin
             GROUP  BY player_id, table_id

This will get the row ids from your table that represent the latest player / table combinations.

Then you use that to pull records from your table, like this (http://sqlfiddle.com/#!2/be68b7/2/0)

SELECT whatever_columns
  FROM poker_buyin
 WHERE buyin_id IN
       (
            SELECT  MAX(buyin_id) buyin_id
              FROM  poker_buyin
             GROUP  BY player_id, table_id
       )
 WHERE player_id = '$player_id'
 ORDER BY player_id, table_id

There's a little trick in this query: The buyin_id value continually goes up, so it's a nice way of selecting the latest-in-time records for each combination.

if you don't need buyin_id in result columns that is simple:

SELECT DISTINCT player_id, table_id, max(date_time) as dt 
FROM `poker_buyin ` 
GROUP BY player_id, table_id