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

  • Home
  • SEARCH
  • 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 6619741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:00:33+00:00 2026-05-25T21:00:33+00:00

I have a single MySQL table with login data of users. user | date

  • 0

I have a single MySQL table with login data of users.

user |  date               | type
-----+---------------------+------
1    | 2011-01-05 08:00:00 | login
1    | 2011-01-06 09:00:00 | login
1    | 2011-01-06 10:00:00 | logout
1    | 2011-01-06 09:50:00 | login

Given the above table I would like to calculate the time difference between the logout date and the previous login date by adding a new cell called duration. E.g. the logout date was ‘2011-01-06 10:00:00; and the previous login date would be ‘2011-01-06 09:50:00’.

The result should look somehow like this. Rows with type=login should not have a duration value.

user |  date               | type   | duration
-----+---------------------+--------+----------
1    | 2011-01-05 08:00:00 | login  | -
1    | 2011-01-06 09:00:00 | login  | -
1    | 2011-01-06 10:00:00 | logout | 10min
1    | 2011-01-06 09:50:00 | login  | -

Thanks in advance,
mawo

  • 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-25T21:00:33+00:00Added an answer on May 25, 2026 at 9:00 pm
    SELECT x.*, TIMEDIFF(x.logout_date, x.login_date) as duration
    FROM
    (
    SELECT a.user_id, a.`date` as logout_date, 
    (SELECT MAX(b.`date`) FROM table1 b WHERE b.`date` <a.`date` 
    and b.user=a.user and b.type = 'login') as login_date    
    FROM table1 a WHERE a.type ='logout'
    )x
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL table containing userID , score . A single user can
I have a mysql table with my users... each user has various attributes like
I have a single user java program that I would like to have store
So I have this function that takes all the users from an MySql table
I have three tables, table one (tableA) containing users data like name and email,
I have MySQL table with the following structure: +---------+--------------+------+-----+---------+----------------+ | Field | Type |
I have a MySQL table using the UTF-8 character set with a single column
I have a list of items in a MySQL table. The user is able
I have a custom made web-site in Python/MySQL with custom user/session tables and users
I have a simple MySQL table like this: create table `users`( `id` int(5) NOT

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.