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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:58:43+00:00 2026-06-11T14:58:43+00:00

i have a mysql table in the following format ————————————————————————————————————- | event_id | event_type

  • 0

i have a mysql table in the following format

     -------------------------------------------------------------------------------------------------------------
    | event_id | event_type | machine    | user      |  timeStamp          | Shift   | Reason             | Count |
     -------------------------------------------------------------------------------------------------------------
    | 101      | Up         | Machine1   | operator1 | 2012-09-06 01:03:55 | Shift1  | Start of The shift | 1     |
    | 102      | Up         | Machine2   | operator2 | 2012-09-06 01:03:55 | Shift1  | Start of The shift | 1     |
    | 103      | Up         | Machine3   | operator3 | 2012-09-06 01:03:55 | Shift1  | Start of The shift | 1     |
    | 104      | Down       | Machine1   | operator1 | 2012-09-06 02:03:55 | Shift1  | Break Down         | 1     |
    | 101      | Up         | Machine1   | operator1 | 2012-09-06 02:33:55 | Shift1  | After BD           | 1     |
     -------------------------------------------------------------------------------------------------------------

by passing the following query.

  $data = mysql_query("select * from rpt_machine_log where (timestamp between date_sub(now(),INTERVAL 2 WEEK) and now() && (machine='machine$i') && (shift='Shift1') ) AND (reason='Start of The Shift' OR reason='End of Shift') ORDER BY 'even_id'") 

i get the following output

     -------------------------------------------------------------------------------------------------------------
    | event_id | event_type | machine    | user      |  timeStamp          | Shift   | Reason             | Count |
     -------------------------------------------------------------------------------------------------------------
    | 101      | Up         | Machine1   | operator1 | 2012-09-06 01:03:55 | Shift1  | Start of The shift | 1     |
    | 118      | Down       | Machine1   | operator1 | 2012-09-06 06:00:55 | Shift1  | End of The shift   | 1     |
    | 127      | Up         | Machine1   | operator1 | 2012-09-07 01:03:55 | Shift1  | Start of The shift | 1     |
    | 148      | Down       | Machine1   | operator1 | 2012-09-07 02:03:55 | Shift1  | End of The shift   | 1     |
     -------------------------------------------------------------------------------------------------------------

I calculate Total Shift Time by finding the difference of the End of The shift and Star of The shift like the following code.

     while ($row = mysql_fetch_assoc($shifttime{$i})) 
            {
                if(isset($timestamp))
                {
                    $diff_shift_time{$i}= strtotime($row['timestamp']) - strtotime($timestamp);
                    $diff_shift_time_array{$i}[]=$diff_shift_time{$i};
                    unset($timestamp);
                }
                else
                {
                    $timestamp=$row['timestamp'];
                }
            }

            foreach ($diff_shift_time_array{$i} as &$value){
                $Total_shift_time{$i} += $value;
                }

in some cases the output of the query is as follows.

     -------------------------------------------------------------------------------------------------------------
    | event_id | event_type | machine    | user      |  timeStamp          | Shift   | Reason             | Count |
     -------------------------------------------------------------------------------------------------------------
    | 121      | Up         | Machine1   | operator1 | 2012-09-06 06:03:55 | Shift2  | Start of The shift | 1     |
    | 124      | Down       | Machine1   | operator1 | 2012-09-06 01:00:55 | Shift2  | End of The shift   | 1     |
    | 151      | Up         | Machine1   | operator1 | 2012-09-07 06:03:55 | Shift2  | Start of The shift | 1     |
     -------------------------------------------------------------------------------------------------------------

when i calculate the total shift time, i get negative value, because my resultant table ends with Up event.

Since the calculation is for consecutive up and down(so the no of rows of the resultant row should always be even), the last up doesn’t have down event to perform calculation.

I want to delete the last row of the table if the row count is odd.

How can i achieve it.

  • 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-11T14:58:44+00:00Added an answer on June 11, 2026 at 2:58 pm

    try

     $count = mysql_num_rows($shifttime{$i});
     $remove_last = $count%2==0?false:true;
     $cur_key = 0;
     while ($row = mysql_fetch_assoc($shifttime{$i})) 
                {
                    if(TRUE === $remove_last && $cur_key == ($count-1))
                          break;
                    $cur_key++;
    
                    if(isset($timestamp))
                    {
                        $diff_shift_time{$i}= strtotime($row['timestamp']) - strtotime($timestamp);
                        $diff_shift_time_array{$i}[]=$diff_shift_time{$i};
                        unset($timestamp);
                    }
                    else
                    {
                        $timestamp=$row['timestamp'];
                    }
                }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a mysql log table of machine in the following format. --------------------------------------------------------------------------------------------------- |
I have a table with the following format. mysql> describe unit_characteristics; +----------------------+------------------+------+-----+---------+----------------+ | Field
I have a mysql database with a table in the following format: ID: 1
I have the following MySQL query, and PHP code to format the Count result
i have a Mysql table shift_def in the following format. +-------------------------------------------------------------------------------+ | id |
I have a MySQL table with the following data (simplified): INSERT INTO `stores` (`storeId`,
I have the following Mysql Table Order table status 0 - incomplete status 1
I have the following MySQL table structure: num field company phone website 1 Gas
I have a following mysql table with a lot (3000+) of values: [id] [parent_id]
I have a MySQL table with a structure like the following: I'm looking for

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.