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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:43:06+00:00 2026-05-21T19:43:06+00:00

I am working on customized shopping cart application. It have a hits table: id

  • 0

I am working on customized shopping cart application.

It have a hits table:

id            int
product_id    int
hit_time      datetime
session_id    varchar

Now when someone views a product, I record a hit.

I need a query such that, record should be inserted only if hit_time has a difference of 30 mins (for same product_id, and session_id)

For example:

When recording first hit

id             product_id             hit_time                session_id
------------------------------------------------------------------------
1                  1                  2011-01-01 06:30:00      abcxyzmno
2                  5                  2011-01-01 06:32:00      abcxyzmno

Now, if visitor with same session_id, visit product page with product_id =1 in less than 30 mins, then data should not be inserted

So below row should not be inserted (because there is difference of 5 mins only)

3                  1                  2011-01-01 06:35:00      abcxyzmno

But if visitor visit after 30 mins, for same product, it should be recorded

3                  1                  2011-01-01 07:01:15      abcxyzmno

I want single insert query

I DON’T want 2 query, first to select max-time from table, and then insert it

Any help?

  • 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-21T19:43:07+00:00Added an answer on May 21, 2026 at 7:43 pm

    Are you looking for something like this?

    INSERT INTO hits (product_id, hit_time, session_id)
    SELECT $product_id, current_timestamp, $session_id
    FROM hits
    WHERE NOT EXISTS (
            SELECT id
            FROM hits
            WHERE product_id = $product_id 
              AND session_id = $session_id
       )
       OR (
                product_id = $product_id
            AND session_id = $session_id
            AND hit_time   < current_timestamp - 30*60
       )
    LIMIT 1
    

    Where $product_id and $session_id are the product and session IDs.

    The NOT EXISTS part takes care of the case where (product_id,session_id) hasn’t been recorded yet and the second part takes care of the case where (product_id,session_id) is there but it was longer than 30 minutes ago. And, of course, the LIMIT 1 in case (product_id,session_id) is there and older than 30 minutes ago and appears multiple times.

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

Sidebar

Related Questions

I'm working on implementing a customized searchBar for a fairly complex table and have
I've been working on a C# ASP.Net application that requires images to be customized
I have customized the camera with custom buttons(UIButton) by overlay view. My iPhone application
I am working on an application in which uitableviewcells of uitableview are customized which
Currently i am working on a iPhone Application Which is displaying video. I have
I am working on an application for work and I need a customized messagebox
The application that I'm working on, has a main customized WPF window. There is
I have customized pages in magento, it was working fine until I installed Extension
I'm working with an application that allows the customer to customize what shortcut keys
Working on a project at the moment and we have to implement soft deletion

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.