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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:37:00+00:00 2026-06-04T19:37:00+00:00

I have a (MySQL) table with two date/time fields: start and end (each of

  • 0

I have a (MySQL) table with two date/time fields: start and end (each of the type datetime). I need a query which delivers me the lowest value from start and the highest value of end for a certain user and a defined date. I finally suceeded with the following query:

SELECT job.id, job.user_id, job.start, last.end 
FROM job
    JOIN job AS last 
        ON job.user_id = last.user_id 
            AND DATE( job.start ) = DATE( last.start ) 
            AND last.end = (SELECT max( last2.end) 
                            FROM job AS last2 
                            WHERE last2.user_id = last.user_id 
                                AND DATE( last2.end ) = DATE( last.end ))
WHERE job.user_id = 1 
    AND DATE( job.start ) = '2012-05-28' 
ORDER BY job.start ASC 
LIMIT 0,1

Even though it works, it does not ‘feel’ right. Is there an easier way to do this?
Any help and hints is/are much appreciated. Thanks!

Example data:

user_id     start               end
1       2012-05-28 07:13:00     2011-04-26 07:45:00
1       2012-05-28 08:15:00     2011-04-26 08:50:00
1       2012-05-28 05:32:00     2011-04-26 05:51:00
1       2012-05-28 08:50:00     2011-04-26 09:50:00
1       2012-05-28 15:10:00     2011-04-26 15:40:00
1       2012-05-27 16:11:00     2011-04-26 16:46:00
2       2012-05-28 09:50:00     2011-04-26 10:35:00
  • 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-06-04T19:37:02+00:00Added an answer on June 4, 2026 at 7:37 pm
    Select user_id
      , Min( start ) As MinStart
      , Max( end ) As MaxEnd
    From job
    Where Date( start ) = '2012-05-28'
      And user_id = 1
    Group By user_id, Date( start )
    

    Based on your comment that {1, 2012-5-28 5:32:00 2011-04-26 15:40:00} is the expected output (which is why sample output is so important), that indicates that the start and end date values do not necessarily have to relate to an actual row’s value. For example, there is no row with the combination of values in your sample data. Given that, the solution is simpler.

    I grouped on Date( start ) only to show that if you remove the Where clause, you can determine the min and max start dates by start day. Given that you are filtering on both, you can simplify the query to:

    Select user_id
      , Min( start ) As MinStart
      , Max( end ) As MaxEnd
    From job
    Where Date( start ) = '2012-05-28'
      And user_id = 1
    Group By user_id
    

    SQL Fiddle verison

    • 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 with two fields called date and time which are
I have a table where each row has a start and stop date-time. These
I have a table called Sessions with two datetime columns: start and end. For
I have a MySQL db with a table containing two fields: check_date (of DATETIME
i work with php/Mysql i have table with two cols (date , cash) with
I need some help with a MySQL query. I have two tables, one with
using php and mysql I have two tables, a users table and a profiles
In a MySQL database I have two tables linked in a join. One table
I am using MYSQL and PHP. I have a table called item. Two of
I have this two tables: mysql> desc vat_rates; +-------------+---------------+------+-----+---------+-------+ | Field | Type |

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.