从列中获取有关另一列数据的数据?

时间:2014-02-25 05:47:49

标签: mysql sql

https://docs.google.com/spreadsheet/ccc?key=0Agh6MIWO2pTcdE0yanV0ZkQwWlR6WGxPQUc1T1YtQmc&usp=sharing

MASTER_ID                           END_DATETIME    media_file
1390962109139_635_787802027.    1/29/2014 5:21:41   d:\songs_new\target uganda\welcome56060_l.wav
1390962109139_635_787802027.    1/29/2014 5:21:48   d:\songs_new\target uganda\pending_e.wav
1390962124349_637_788260286.    1/29/2014 5:21:23   d:\songs_new\target uganda\welcome56060_l.wav
1390962199363_744_789334537.    1/29/2014 5:23:03   d:\songs_new\target uganda\welcome56060_l.wav
1390962199363_744_789334537.    1/29/2014 5:23:06   d:\songs_new\target uganda\lang56060mix_l.wav
1390962199363_744_789334537.    1/29/2014 5:23:16   d:\songs_new\target uganda\sub2121_l.wav
1390962199363_744_789334537.    1/29/2014 5:23:03   d:\songs_new\target uganda\welcome56060_l.wav
1390962199363_744_789334537.    1/29/2014 5:23:06   d:\songs_new\target uganda\lang56060mix_l.wav
1390962199363_744_789334537.    1/29/2014 5:23:16   d:\songs_new\target uganda\sub2121_l.wav
1390962205310_742_787802027.    1/29/2014 5:23:13   d:\songs_new\target uganda\welcome56060_l.wav

请查看示例表。 我需要获取相应media file

的最后一次播放Master_ID

例如......

主标识1390962109139_635_787802027所需的数据为d:\songs_new\target uganda\pending_e.wav

等等......

我正在使用MySQL服务器5.5 SQL Yog

3 个答案:

答案 0 :(得分:0)

因为它是mysql,你可以使用它的时髦分组:

select * from (
    select master_id, media_file
    from mytable
    order by end_datetime desc
) x
group by 1

仅通过master_id进行分组意味着mysql将为每个master_id返回第一个行。首先对数据进行排序意味着返回的行将是最新的行。

答案 1 :(得分:0)

使用JOINS

SELECT * 
FROM sample_table AS s1
LEFT JOIN (
            SELECT MAX(END_DATETIME)
            FROM sample_table 
            GROUP BY MASTER_ID
           ) AS max_master_id
ON (s1.END_DATETIME = max_master_id.END_DATETIME);

答案 2 :(得分:0)

尝试以下查询:

SELECT * FROM (
                 SELECT MASTER_ID,
                        media_file
                 FROM my_table 
                 ORDER BY end_datetime DESC
               ) a
 GROUP BY MASTER_ID;