简体   繁体   中英

MySQL select first instance from group of rows

I have a table that stores user visits like so:

visits (id, pageURL, date, ip)

Each time a user goes to a page a new row is created, so each user will have many rows all linked together through their IP.

I'm trying to come up with a query that will select only the first row each user landed on the first time they came to the site as long as the pageURL is user.html.

This is what I can't figure out, I can easily do it with PHP and a couple of MySQL queries, but It might be quicker if there is just one query doing it.

就在我的头顶上,我在想类似

SELECT * FROM visits WHERE pageURL='user.html' GROUP BY ip SORT BY ip, date
SELECT pageURL,date,ip 
FROM visits v1
WHERE id = (SELECT MIN(id) FROM visits v2 WHERE v2.ip = v1.ip) 
  AND pageURL='user.html' -- dont know if necessary

http://sqlfiddle.com/#!2/9b669/5

select * from visits group by ip having pageURL='user.html' order by id,date asc limit 1

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