Time counter MSQl - mysql

I have a column of TIME type in my db, the column is supposed to hold the sum of two other TIMEs, the problem is that when the number of hours of the result exceeds 24 hours the column is reseted to 00:00:00 again, instead of viewing 25:00:00 which is the result that I want to see, any help on how can I get that without changing the column type?

If you want to store numbers that are bigger than a possible time then that data type is inappropriate.
Use another way to store the data like the sum of seconds. You could use an unsigned int for that.

Related

Database Schema Design for different timesheet format

Could someone help to understand the efficient way of storing the timesheet data (Timesheet_Details)?
The timesheet data can be input in multiple ways
Start time and end time (9am to 5 pm)
Plain input of hours (8 hours)
Input like hours in multiples of 2 (2,4,8)
Based on unit like e.g. 1 = full day, 0.5 = half day.
What I have is a master Timesheet table which will store the start and end date for the job. But I am confused how to store the timesheet hours against each date.
Timesheet
id
client_uuid
user_uuid
job_uuid
start_date
end_date
Timesheet_Details
id
timesheet_uuid (FK)
date
?
?
?
You say that the timesheet data can be input in multiple ways (the four mentioned). Is it something you need to have in the interface of the application? Is it something you really need to duplicate in the database? What I mean is that even though the user experience that is asked is to be able to enter the time in multiple different ways, it does not need to be stored this way, the data can be transformed in a way that is consistent in the database, and then you would show the format according to the user preferences. You would use some algorithm to transform the data and store it correctly in the database.
For instance, you could add a table that contains all the possible ways of showing the time, listing the four that you mentioned here but with codes, let's name it "time_presentation". The interface would show a dropdown and the user would select a way to enter the time. The interface would change according to what they want (for example, two inputs to enter beginning and end, one input to enter only the hours, etc.). Then, the data would be sent to the backend and stored in the Timesheet_Details table but always with the same format that you will have calculated with the code, for example, a float column containing the total hours, and a column containing a foreign key to the table "time_presentation". Then, to show the item in the interface, you would be able to use both columns and reverse the process to show it according to what the user entered and how they entered it (if it's important the user sees it as entered).
If it's absolutly necessary to store the data directly with the 4 methods mentioned, then I would suggest using a varchar column with the value (but this would be a lot less easy to validate).

Updating mysql table whilst shifting values

I have a table that I use for statistical purposes.
Its columns are id and 1,2,3,..,31 and pivot.
This table gives the number of views on each day for the last 31 days.
1 gives the number of views for yesterday.
14 gives the number of views for 14 days ago.
etc ...
(pivot is just used to calculate the number of views)
I would use a cron job every day to update this table, but how would I go about "shifting" all the values to the side ( value column 15 would become value column 16; new value for column 1; delete value for column 31)
Define a table with only two columns — "date" and "views"
INSERT a new row in the table with the view count for that day when the CRON job runs
Modify your application query to read through this new table over a custom date range, which could be 31 days or anything else either — please have a look at this link to get an idea:
MySQL Query - Records between Today and Last 30 Days
Not really sure how pivot is being used here. However, I'm almost certain that if you're using it to store the sum of the views, it could as well be computed by using SUM() or GROUP BY without having to need a separate column in the table
As far as data archival / removal is concerned, your daily CRON job could be modified to include a DELETE query (as the last step) which cleans up records older than a certain date. Again, you could use the link above to get your "target" date
.
I apologise that this might sound like a little too long a solution to what you've asked for. However, I feel, this approach should help you organise and maintain the table in question in a better way.

Fill column to max value in MySQL?

I want to know if its possible to automatically fill a column to its max value if the value inside it isn't already at the max value. For example, if I have a column called score that takes an INT(11) and the data in that column is just 23, is there a way I can get it to add a bunch of numbers to the end of it so its size is actually 11 and not 2? so in the end it would look something like 23000000000?
What you ask about is called padding. You could do that at the database schema level, using a varchar column, but I suggest against it for several reason, including no auto-increment numbers. You can easily pad the number as you like while you query the database, for instance using:
SELECT LPAD('columnname',11,'0')

MYSQL Time Comparison Range

I'm trying to help a taxi company. The problem is that they have a credit card machine that takes payments, and has its own database entries, and there is a completely separate database that has a list of entries such as pick up time, and drop off time.
I need to match the database of trip entries to the credit card purchases, and the only way to do this is by matching which vehicle is running the transaction, and looking for a time CLOSE TO the DROP OFF time and see if it's a match. It's not ideal.
So, I am trying to compare two times in yy-mm-dd hh:mm:ss format. They need to be plus or minus 5 minutes of each other. How do I do this in MYSQL?
I thought SubTime and Addtime would work, and it seemed to, but then I got wierd results.
SELECT * FROM completedtrans WHERE DrivID = 128 AND TransTime BETWEEN SUBTIME('2013-06-20 16:53:06', '0 00:05:00') AND ADDTIME('2013-06-20 16:53:06', '0 00:05:00')
Here's an example of one of my searches. Can anyone tell me what's wrong with it? It's supposed to search 5 minutes before and after that particular given time. I can't simply write the time, because the query is automatically generated through php code.

php mysql storing just hours minutes seconds

I know a typical timestamp in any format readable or otherwise is always equivalent to a date time second day month year etc. However I want to be able to search by hours minutes seconds where the day month year are irrelevant. So I am trying to wrap my head around that ability and what would be the best method of storing time so I can create searches around that alone without m-d-y getting in my way.
Try using the TIME field type. The TIMESTAMP field type should only be used anyway when you want MySQL to update the field when updating the row.
$hour = date("H",$date); $minute = date("i",$date); $second = date("s",$date);
and save them on your table as hour,minute and second