mysql/mariadb date column time add to get total time [duplicate] - php

This question already has answers here:
MySQL - How to SUM times?
(5 answers)
Closed 5 years ago.
I am working on a check in out front desk register and hit a wall when attempting to add the total times for all visits by individuals or companies: when i run the following query:
$yearStart = date('Y-m-d', strtotime(date('Y-01-01')));
$today = date("Y-m-d",time());
$sql = "SELECT visCompany, count(visCompany) as accumilatedVisits, sum(timeVisited) as total_time from cards where cards.dateOfVisit BETWEEN '$yearStart' AND '$today' GROUP BY cards.visCompany ORDER BY accumilatedVisits DESC";
I get the sum of the two numbers but not in the correct format, but I cannot figure or find a better way to do it... I had at the start the following times saved in my Cards table: 00:29:18 and 00:05:43 which when added together give me 3461 which would be correct-ish if you put a space between 34 and 61, the total should be 00:35:01 and the values seem to be there but i would think that there is a cleaner way of accomplishing the correctly added and formatted values... I have been scowering the mariadb kb and even trying to find the solution here... I would be okay figuring out a way to do the formatting and the minute correction in php too.. but would prefer to use sql to get the desired result.
Any one have any suggestions?

Whops i put a duplicate... found the answer:: MySQL - How to SUM times?
my new query:: SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(cards.timeVisited))) as total_time_visited FROM cards;
tested and worked with the result being 00:35:01.
Sorry about the duplicate... And thanks for reading.

Related

How do I use LIMIT and OFFSET properly in PDO MySql [duplicate]

This question already has answers here:
How do I calculate the offsets for pagination?
(4 answers)
How to apply bindValue method in LIMIT clause?
(11 answers)
Closed 5 years ago.
I asked a question yesterday titled "How do i use binded variables with MySql LIMIT clause. Sorry, I guess it was not clear enough and got 30 views but no suggestions. So here is what I found after a day of trial and error. First, I discovered that the the LIMIT is the actual physical position of a record in a table not my auto_incremented id_ number. A lot of documentation i read also indicated it was the OFFSET that held the record position, so I wasted a lot of time with weird results. Second, I found documentation with a couple of ways to bind a variable to LIMIT. None of them worked, so I resorted to assigning a $variable to the LIMIT as follows.
("SELECT * FROM $livetable WHERE cust_zip = :cust_zip
HAVING distance < :mydistance ORDER BY client_id ASC LIMIT $start_record_position
, $how_many_records_to_display");
This works for me but any better suggestions, would be appreciated. And I hope this will save someone else a day of figuring this out.

Mysql checking for avaliable time between opening times

I am desperate, I read many posts but couldn't quite find a solution...
I have a table called reservations.
In there I have 5 fields
ID(int)
room(TEXT)
date(DATE)
from(TIME)
to(TIME)
lets say in PHP I have 4 variables:
$room = "sauna";
$date = "2015-03-03";
$open_from = "09:00:00";
$open_to = "15:00:00";
I don't know how to make this Query which tells me whether there is still time for use between the opening times or not
I read many threads but I couldn't find anything which has this exact thing.
All I need is a '0' or '1' as a return value so I can simply dynamically remove the "add reservation" button from the page if there's no time left where one could put in a reservation anyways...
Please help me :(
Sincerely
Depth

codeigniter active record querying for results based on hours as well as by state

I have two queries ultimately I think they will be in the same context of the other but in all. I have a user database that I want to pull out for tracking records based on hour. Example registrations per hour. But in this registrations per hour I want to have the query to dump results by hour increments (or weeks, or months) ie: 1,000 regitrations in november, 1,014 in december and so on, or similar for weeks hours.
I also have a similar query where I want to generate a list of states with the counts next to them of how many users I have per state.
My issue is, I'm thinking I think to one dimensionally currently cause the best idea I can think of at the moment is making in the case of the states 50 queries, but I know thats insane, and there has to be an easier way thats less intense. So thats what Im hoping someone from here can help me with, by giving me a general idea. Cause I don't know which is the best course of action for this currently.. be it using distinct, group_by or something else.
Experiment a bit and see if that doesn't help you focus on the question a bit more.
Try selecting from your registrations per hour table and appending the time buckets you are interested in to the select list.
like this:
select userid, regid, date_time, week(date_time), year(date_time), day(date_time)
from registraions;
you can then roll up and count things in that table by using group by and an aggregate function like this:
select count(distinct userid), year(date_time)
from registraions
group by year(date_time)
Read about about date time functions:
MySQL Date Time Functions
Read about aggregate functions"
MySQL Group By

MySQL replace field entry with substring [duplicate]

This question already has answers here:
Converting a date in MySQL from string field
(4 answers)
Closed 9 years ago.
I currently have dates stored in my database (in string format) currently as mm/dd/yyyy.
I need to replace all entries as yyyy/mm/dd for sorting purposes.
To clarify, I am a PHP developer but my raw MySql isnt as strong. I was thinking of doing this by using substrings to re-arrange the date. (I tried in PHP first but it take way too long to process this much data, about a half million rows).
Am i going about this correctly? if so, how do I "loop through" the data, assigning my substr variables for the current iteration?
Note: I did try str_to_date here and it ended up nullifying half of the fields. Im not entirely sure why.
Your help is greatly appreciated.
this is an insert statment
INSERT INTO yourtable (datefield) VALUES (str_to_date(date, '%Y/%m/%d'));
and this an update
UPDATE Table SET date=STR_TO_DATE('date','%Y/%m/%d')
try this
UPDATE Table SET date = DATE_FORMAT('date', %Y/%m/$d);
I believe this will work if your column is in a consistent mm/dd/yyyy format
UPDATE mytable
SET date = CONCAT(right(date,4), '/', left(date,5))

MySQL Queries before NOW

Now I've read on this fantabulous site about how to check if the timestamp that is in your database is before now(), and that answers part of my question.
But the remainder of my question still eludes me:
How can you not only check for timestamps BEFORE now(), but also put a cap on how many before now are queried?
I have a database that is acting as a guestbook, and the output is a flash piece that looks like a post-it board. you fill out a form, and the submission immediately "tacks" itself to the post-it board. The problem is that when you first load the page, it will load every single post starting from id-0 to id-5,000,000 and so on and so forth.
I would like to put a cap on how many are loaded so the query looks at the following things:
What's the current timestamp?
Go back in time (for example) 10 entries ago
Post from THAT point on
The fields I have in my database are: id, comments, timestamp
EDIT
I'm looking at some of the answers, and I would like to ask about the LIMIT. If I put a limit on the queries, will I still be able to query posts that are PAST now?
Example: Say there are two people viewing the site at the same time. One visitor posts a comment into the database. I want person 2 to still be able to the see that comment pop up on his end.
The flash post-it area runs off a php script that queries the database and exports it into an XML format that Flash can read. Every 5 seconds, the flash re-queries the database to check for more posts.
If I put the Limit on the query, will I still be able to grab NEW entries on the fly? I just want the LIMIT to generate a starting point offset from ground zero
I think what you are looking for is called Limit
You just put it at the end of your statement and the query will return the amount of results you wanted
YOUR QUERY LIMIT 0,10
This will return 10 first results
SELECT * FROM
(SELECT ... WHERE dt<NOW() ORDER BY dt DESC LIMIT 10) a
ORDER BY a.dt ASC
or
SELECT ... WHERE dt<NOW() ORDER BY dt DESC LIMIT 10
check which is the more suitable for you.

Categories