Php 仅当下一行不相同时才选择行

Php 仅当下一行不相同时才选择行,php,mysql,Php,Mysql,我有一个表,有歌曲代码和时间戳。代码表示正在播放的歌曲,时间戳是识别歌曲播放的时间。我需要生成一个所有代码及其相应时间戳的列表,但仅在第一次识别它们时。主要的问题是一首歌可能会播放多次,唯一能表明它是一首歌的新实例的是前一行包含不同的代码 这是我的代码(它只给出每一个的第一次和最后一次代码) SELECT a.* FROM ap_results a INNER JOIN ( SELECT trackId, MAX(timeplay), timeplay FROM ap_results GROUP

我有一个表,有歌曲代码和时间戳。代码表示正在播放的歌曲,时间戳是识别歌曲播放的时间。我需要生成一个所有代码及其相应时间戳的列表,但仅在第一次识别它们时。主要的问题是一首歌可能会播放多次,唯一能表明它是一首歌的新实例的是前一行包含不同的代码

这是我的代码(它只给出每一个的第一次和最后一次代码)

SELECT a.* FROM ap_results a INNER JOIN ( SELECT trackId, MAX(timeplay), timeplay FROM ap_results GROUP BY trackId ) b ON a.trackId = b.trackId AND a.timeplay > b.timeplay WHERE a.trackId <> b.trackId ORDER BY timeplay
我需要一个查询来显示以下内容:

42390-01 - 2016-02-01 01:00:00
54212-04 - 2016-02-01 01:01:30
42390-01 - 2016-02-01 01:02:08
12899-03 - 2016-02-01 01:07:02
42390-01 - 2016-02-01 01:08:10

我认为您所寻找的是sql不容易获得的,因为这是一个过程性的结果

下面是一些简短的建议(使用pesudo代码,因为我不知道您使用哪种服务器端语言),用于通过过程方法从服务器端获取结果

connect your db 

get all the rows in order of timeplay 

set the firts rows in a var (actRow) and in an array of result rows (resultRows[])

loop over these rows starting from the second  {
     if the actual in loop  row trackId is not the same of actRow. {
             store/add this row in resultRows // for final result 
             and set the var for row with this value
      }
}

show  the resultRows

您在此处同时标记了
mysql
sql server
,并且您可能不会同时使用这两个数据库。它是哪一种RDBMS?请始终提及数据库版本。如前所述提及sq server或mysqlsorry,这只是mysqlthat querry返回歌曲代码的第一个实例。如果歌曲稍后在那天它没有返回结果。很抱歉,我已经理解了这是你的需要。请更好地解释你想要的是foirts还是last..timeplay?我对此非常陌生,但它看起来只是将trackId的所有实例分组并返回最早的一个。不要使用注释..尝试编辑问题并正确格式化数据..是吗更好..我试图插入表,但没有成功。下面是一个例子。一首歌(歌1)在上午10:00播放,这被写入数据库,在10:01它再次被识别为正在播放,这也被写入数据库。在10:02一首新歌(歌2)被识别为正在播放并写入DB,10:03和10:04时再次播放并写入DB。10:05时,歌曲1再次播放并写入DB。此时有5个条目,但我想显示三个结果,以表示每次播放歌曲的时间。分别为10:00、10:02和10:05
connect your db 

get all the rows in order of timeplay 

set the firts rows in a var (actRow) and in an array of result rows (resultRows[])

loop over these rows starting from the second  {
     if the actual in loop  row trackId is not the same of actRow. {
             store/add this row in resultRows // for final result 
             and set the var for row with this value
      }
}

show  the resultRows