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 6860719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:26:38+00:00 2026-05-27T02:26:38+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 to a users profile but I only want to display the visitor 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 = 2 AND v.viewer_id != 2
 ORDER BY created_at DESC LIMIT 0,20;

The result set:

+------+---------+-----------+---------------------+---------------------+
| id   | user_id | viewer_id | created_at          | updated_at          |
+------+---------+-----------+---------------------+---------------------+
| 1314 |       2 |         1 | 2011-11-20 18:50:35 | 2011-11-20 18:50:35 |
| 1311 |       2 |         1 | 2011-11-17 00:51:10 | 2011-11-17 00:51:10 |
| 1307 |       2 |         1 | 2011-11-16 19:28:51 | 2011-11-16 19:28:51 |
| 1301 |       2 |         1 | 2011-11-16 19:08:08 | 2011-11-16 19:08:08 |
| 1299 |       2 |         1 | 2011-11-16 17:46:47 | 2011-11-16 17:46:47 |
| 1295 |       2 |         1 | 2011-11-16 17:34:09 | 2011-11-16 17:34:09 |
| 1277 |       2 |         1 | 2011-11-16 16:54:11 | 2011-11-16 16:54:11 |
| 1270 |       2 |         1 | 2011-11-16 15:51:09 | 2011-11-16 15:51:09 |
| 1109 |       2 |         1 | 2011-11-01 20:56:39 | 2011-11-01 20:56:39 |
| 1108 |       2 |         1 | 2011-11-01 20:56:34 | 2011-11-01 20:56:34 |
| 1100 |       2 |         1 | 2011-11-01 18:48:06 | 2011-11-01 18:48:06 |
| 1098 |       2 |         1 | 2011-11-01 18:43:44 | 2011-11-01 18:43:44 |
| 1097 |       2 |         1 | 2011-11-01 18:43:34 | 2011-11-01 18:43:34 |
|  950 |       2 |         1 | 2011-10-05 20:07:37 | 2011-10-05 20:07:37 |
|  948 |       2 |         1 | 2011-10-05 19:39:03 | 2011-10-05 19:39:03 |
|  944 |       2 |         1 | 2011-10-05 19:12:02 | 2011-10-05 19:12:02 |
|  941 |       2 |         1 | 2011-10-05 19:06:35 | 2011-10-05 19:06:35 |
|  935 |       2 |         1 | 2011-10-05 18:40:17 | 2011-10-05 18:40:17 |
|  933 |       2 |         1 | 2011-10-05 18:18:31 | 2011-10-05 18:18:31 |
|  932 |       2 |         1 | 2011-10-05 18:14:08 | 2011-10-05 18:14:08 |
+------+---------+-----------+---------------------+---------------------+
20 rows in set (0.00 sec)

However I only want to return the visitor once ordered by their last visit date. So if a user visits a users profile three times in a row, i’ll have 3 records in the user_profile_view_table but I only want to return the record of their last visit.

I need to use Group-by-N for this but i’m stuck.

This is what I have currently thought of:

SELECT v.viewer_id FROM user_profile_view v 
WHERE v.viewer_id != 2 AND v.user_id = 2
AND NOT EXISTS (SELECT * FROM user_profile_view v2
                WHERE v2.viewer_id = v.viewer_id AND v2.created_at < v.created_at)
ORDER BY created_at DESC;
  • 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-27T02:26:38+00:00Added an answer on May 27, 2026 at 2:26 am
    SELECT v.viewer_id, MAX(created_at) AS MaxCreated
        FROM user_profile
        WHERE viewer_id != 2
            AND v.user_id = 2
        ORDER BY MaxCreated
        GROUP BY v.viewer_id;
    
    • 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.