简体   繁体   中英

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.

I need my backend script to display the latest page that an IP address has visited.

My database is setup like this:

在此处输入图片说明

I want to be able to use all of the columns, such as "browseid" and "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.

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:

 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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