简体   繁体   English

选择“唯一/唯一”,但返回所有列

[英]Select Distinct/Unique but return all columns

I have a script setup to report a bunch of data periodically, such as IP address, and the page a visitor is on. 我有一个脚本设置,可以定期报告大量数据,例如IP地址和访问者所在的页面。

I need my backend script to display the latest page that an IP address has visited. 我需要后端脚本来显示IP地址已访问的最新页面。

My database is setup like this: 我的数据库设置如下:

在此处输入图片说明

I want to be able to use all of the columns, such as "browseid" and "page". 我希望能够使用所有列,例如“ browseid”和“ page”。 I want to retrieve the last value only. 我只想检索最后一个值。 To give you a frame of reference, using the example data above, I'd only want to retrieve record 60 , because it's the last page that the IP xxx.xx.xxx.xx visited. 为了给您一个参考框架,使用上面的示例数据,我只想检索记录60 ,因为它是IP xxx.xx.xxx.xx访问的最后一页。

I've been trying to find a solution that works for the last hour, but I just can't get one to work. 我一直在寻找一个可以在最后一个小时使用的解决方案,但我只是无法解决这个问题。 I'm also trying to select data within a certain timeframe. 我也在尝试在特定时间范围内选择数据。 Here's what I've been using to check for that: 这是我一直在检查的内容:

WHERE ts >= '" . strtotime("-15 minutes") . "' AND expiry <= '" . time() . "'

You could use a join on the max id groped by ip eg: 您可以对ip摸索到的最大ID使用连接,例如:

 select * from my_table m 
 inner join  (
    select clientip, max(browseid) as m_id 
    from my_table
    group by clientip
 ) t on t.clientip = m.clientip and t.m_id = m.browseid

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

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