简体   繁体   English

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

[英]mysql select all unique rows in one column and all max rows in another column by datetime

What I need is to get the most recent (by date_time) unique player_id for each table_id 我需要为每个table_id获取最新的(按date_time) unique player_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 我尝试过这样的事情,每个table_id只返回1行而不是1行

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. 您提到的查询将查找每个玩家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. 这将从表中获取代表最新播放器/表组合的行ID。

Then you use that to pull records from your table, like this ( http://sqlfiddle.com/#!2/be68b7/2/0 ) 然后使用它从表中提取记录,例如( 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. 该查询中有一个小技巧:buyin_id值不断上升,因此这是为每个组合选择最新记录的好方法。

if you don't need buyin_id in result columns that is simple: 如果在结果列中不需要buyin_id ,则很简单:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM