我有一个图像表和图像视图状态表。
在每次单击图像时,我都在跟踪图像查看时间。状态。日期时间..因此,在图像视图状态表中,一张图像有多个条目。
我需要检查一个用户是否已经查看过一张图像。
所以我这样写一个查询。select A.title, if(B.view_status='completed','completed','notviewed') from images A left join imgstatus_view B on (A.imgId=B.ImgId and A.fileId=B.fileID) Where userId=1 and imgId=121
如果我查看一张图像三倍,则它获取3条记录。我只需要一条记录即可检查用户是否查看了图像。
因此,在不使用DISTINCT
或GROUP BY
的情况下,如何获取唯一的数据行。
请帮我。
最佳答案
您可以使用ORDER BY和LIMIT子句。要获取最新记录,可以在下面的查询中使用
select A.title, if(B.view_status='completed','completed','notviewed') from images A left join imgstatus_view B on (A.imgId=B.ImgId and A.fileId=B.fileID) Where userId=1 and imgId=121 ORDER BY datetimefield DESC LIMIT 0,1
获得旧唱片
select A.title, if(B.view_status='completed','completed','notviewed') from images A left join imgstatus_view B on (A.imgId=B.ImgId and A.fileId=B.fileID) Where userId=1 and imgId=121 ORDER BY datetimefield LIMIT 0,1
如果您没有datetime字段,请删除ORDER BY子句,该子句将提供最新的修改记录
关于php - 在不使用DISTINCT/GROUP BY的情况下,如何从mysql 5表中获取数据的唯一行?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8585327/