Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6644157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:08:04+00:00 2026-05-26T00:08:04+00:00

I have a user_profile_view table: The definition is as follows: +————+————+——+—–+———+—————-+ | Field |

  • 0

I have a user_profile_view table:

The definition is as follows:

+------------+------------+------+-----+---------+----------------+
| Field      | Type       | Null | Key | Default | Extra          |
+------------+------------+------+-----+---------+----------------+
| id         | bigint(20) | NO   | PRI | NULL    | auto_increment |
| user_id    | bigint(20) | NO   | MUL | NULL    |                |
| viewer_id  | bigint(20) | NO   | MUL | NULL    |                |
| created_at | datetime   | NO   |     | NULL    |                |
| updated_at | datetime   | NO   |     | NULL    |                |
+------------+------------+------+-----+---------+----------------+

I’m trying to get the most recent visits but I only want to display the user once

A simple query such as this will return the most recent visits on a profile:

 SELECT v.* FROM user_profile_view v 
 WHERE v.user_id != 1 AND v.viewer_id = 1 
 ORDER BY created_at DESC LIMIT 0,10;

The result set:

+-----+---------+-----------+---------------------+---------------------+
| id  | user_id | viewer_id | created_at          | updated_at          |
+-----+---------+-----------+---------------------+---------------------+
| 946 |      19 |         1 | 2011-10-05 19:20:19 | 2011-10-05 19:20:19 |
| 945 |      19 |         1 | 2011-10-05 19:16:15 | 2011-10-05 19:16:15 |
| 944 |       2 |         1 | 2011-10-05 19:12:02 | 2011-10-05 19:12:02 |
| 943 |      13 |         1 | 2011-10-05 19:11:45 | 2011-10-05 19:11:45 |
| 942 |      19 |         1 | 2011-10-05 19:10:41 | 2011-10-05 19:10:41 |
| 941 |       2 |         1 | 2011-10-05 19:06:35 | 2011-10-05 19:06:35 |
| 940 |      19 |         1 | 2011-10-05 18:53:04 | 2011-10-05 18:53:04 |
| 939 |      19 |         1 | 2011-10-05 18:51:56 | 2011-10-05 18:51:56 |
| 938 |      19 |         1 | 2011-10-05 18:46:59 | 2011-10-05 18:46:59 |
| 937 |      20 |         1 | 2011-10-05 18:42:35 | 2011-10-05 18:42:35 |
+-----+---------+-----------+---------------------+---------------------+

However I only want to return the user once. So if I have recently viewed a users profile 3 times, I only want the most recent visit to that profile and then display the next user who I visited before.

This should be trivial but for some reason I’m stuck.

Thanks folks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:08:05+00:00Added an answer on May 26, 2026 at 12:08 am

    It’s not actually trivial. This is a “greatest-N-per-group” problem. Some variants of SQL have added windowing functions to handle this, I’m not sure whether MySQL has or not.

    To do this without windowing functions you can test each potential answer to see if there exists in the same table a row for the same user_id but a more recent created_at time):

    SELECT v.* FROM user_profile_view v 
    WHERE v.user_id != 1 AND v.viewer_id = 1 
       AND NOT EXISTS 
         (SELECT * FROM user_profile_view v2 
          WHERE v2.user_id = v.user_id AND v2.created_at > v.created_at)
    ORDER BY created_at DESC LIMIT 0,10;
    

    Note that ties (two records with the exact same created_at value) will return both rows.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a user_profile_view table: The definition is as follows: +------------+------------+------+-----+---------+----------------+ | Field |
Below is my table, a User could have multiple profiles in certain languages, non-English
I have a pretty large social network type site I have working on for
I have the following tables/views Users (View) -> UserId -> Name Roles (Table) ->
I have a user profile table that has 32 fields. These fields are always
I have a social app that has a user_profile table with typical standard fields,
I have 2 tables with some columns and another table that I want its
I have a user profile page on my web app which has contact information.
Dear all, I have a question about Facebook Page: ( NOT user profile page,
I have many many small images that are displayed on a user's profile and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.