How to calculate date time in one field - php

I have a table below named transaction_detail
id_transd | id_trans | id_cust | inputtime |
1 | 1 | 1 | 2013-04-15 16:55:58 |
2 | 1 | 1 | 2013-05-15 16:55:58 |
3 | 1 | 1 | 2013-06-15 16:55:58 |
4 | 2 | 2 | 2013-06-15 16:55:58 |
I want to get the amount of the inputtime (type : datetime) which have the same id_cust. I've done with the date, but I don't know how to calculate the time. This is my sql syntax to calculate the date :
select (date(max(inputtime)) - date (min(inputtime))) as total from transaction_detail where id_cust = '$idp'
Any help will be very appreciated. Thanks before.

Have a look in this website this might help what you need.
http://www.sqlusa.com/bestpractices/datetimeconversion/

Use TIMESTAMPDIFF function to get difference between two datetime values in units you want.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timestampdiff
Units could be: MICROSECOND (microseconds), SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, or YEAR.

Related

need help in sql query with counting values within dates

the table:
tab_tasks
id|....|date_completed|completed
1 | | 2016-11-05 | Y
2 | | 2016-11-07 | N
3 | | 2016-11-09 | Y
4 | | 2016-12-11 | Y
5 | | 2017-01-15 | Y
6 | | 2017-01-30 | Y
and so on...
I need help in writing sql query by giving current date and get months with counted completed tasks back to max 12 months something like:
month | num_complited
november | 2
december | 1
january | 2
or if it would be easier the instead of letters of the alphabet, month can be numeric 11, 12, 01 in column month.
How can be this accomplished? Is there a need for any php code or can be this done with just sql query? Or should I add extra column to that table, like month?
you should put some date range otherwise for example 11-2016 has 6 count and 11-2017 has 5 so out put will show 11 count data mix-up
SELECT MONTHNAME(date_completed) as month ,
count(completed) as num_complited
from tab_tasks
where completed='Y' and
date_completed between '2016-11-05' and '2017-11-05'
group by MONTHNAME(date_completed)
Try this:
select MONTHNAME(date_completed) as MosName,count(1) as num_completed
from TableName
where completed = 'Y'
GROUP BY MONTHNAME(date_completed);

SQL update statement to add one year to access_end date

I can code in PHP but I'm not good with SQL at all. I need to run an update on a table in order to pass in a given user_id and set the "access_end" date for all products the user owns to one year from today's date.
Any help much appreciated
Database is MySQL
Table name is dap_users_products_jn
Relevant Fields in database are:
user_id | access_end_date | product_id
1 | 2012-10-26 | 34
1 | 2012-11-21 | 30
1 | 2012-12-22 | 3
2 | 2012-10-20 | 34
2 | 2012-07-18 | 30
2 | 2012-08-15 | 3
...etc
update dap_users_products_jn
set access_end_date = date_add(now(), interval 1 year)
where user_id = 1

check if a period is included between two dates

i've a mysql table like this:
| ID | ID_period | date_start | date_end |
| 1 | 1 | 0000-07-01 | 0000-08-31 |
| 2 | 2 | 0000-09-01 | 0000-10-30 |
| 3 | 3 | 0000-11-01 | 0000-12-28 |
| 4 | 4 | 0000-11-01 | 0000-03-31 |
how can i select IDs that are included between this period 0000/07/14 - 0000/08/25 ?
date_start and date_end columns are DATE format.
THE PROBLEM is that if i search for a period (included and intersect) that is ie: from 0000-12-12 to 0000-01-25 i get 0 records from the select, i guess for the year that is '0000'.. how can i fix it ?
another problem is that if i search a period like 11-01 to 12-31 i got 0 results.. because last day od december in date_end is 28.. but if i search for a period 11-01 to 12-31 is because i want all the records included.. so i'd like to get the record having id=3 and id=4
at the moment im using the following query:
SELECT ... WHERE '12' BETWEEN MONTH(date_start) and MONTH(date_end)
AND '15' BETWEEN DAY(date_start) and DAY(date_end)
AND '03' BETWEEN MONTH(date_start) and MONTH(date_end)
AND '28' BETWEEN DAY(date_start) and DAY(date_end)
SELECT *
FROM mytable
WHERE date_start BETWEEN '0000-07-14' AND '0000-08-25'
OR date_end BETWEEN '0000-07-14' AND '0000-08-25'
OR (date_start<'0000-07-14' AND date_end>'0000-08-25')

Get total views per day including leaving "0 views" for a day that has no record

I am creating a graph where I can get the total views everyday for a certain range, or as long it goes back.
The problem I am having is to fill a default number of 0 when no views has been made for a certain day, some days there may be absolutely no views in a day so I need MySQL to return a default of 0 when none is found - I have no idea how to do this.
This is the query I use to get the total views a day:
SELECT DATE(FROM_UNIXTIME(v.date)) AS date_views,
COUNT(v.view_id) AS total_views
FROM
(
views v
)
GROUP BY date_views
ORDER BY v.date DESC
My results return this:
+------------+-------------+
| date_views | total_views |
+------------+-------------+
| 2012-10-17 | 2 |
| 2012-10-15 | 5 |
| 2012-10-14 | 1 |
| 2012-10-10 | 7 |
+------------+-------------+
However there are missing days that I want to return 0 for it, as 2012-10-16, 2012-10-11, 2012-10-12, 2012-10-13 is not included.
So, for example:
+------------+-------------+
| date_views | total_views |
+------------+-------------+
| 2012-10-17 | 2 |
| 2012-10-16 | 0 |
| 2012-10-15 | 5 |
| 2012-10-14 | 1 |
| 2012-10-13 | 0 |
| 2012-10-12 | 0 |
| 2012-10-11 | 0 |
| 2012-10-10 | 7 |
+------------+-------------+
Would be returned.
How would this be approached?
When I did this a couple of years ago I created an empty array with the date as key and the default value 0. Then I simply looped through the result att changed the value for those dates I had.
for each($result as $row){
$date_stats_array[$row['date']] = $row['value'];
}
In situations like this I create a temporary table which I fill with all the dates you want. After that, you can use that table to join your original query against.
To fill the table you can use this procedure:
DROP PROCEDURE IF EXISTS filldates;
DELIMITER |
CREATE PROCEDURE filldates(dateStart DATE, dateEnd DATE)
BEGIN
WHILE dateStart <= dateEnd DO
INSERT INTO tablename (_date) VALUES (dateStart);
SET dateStart = date_add(dateStart, INTERVAL 1 DAY);
END WHILE;
END;
|
DELIMITER ;
CALL filldates('2011-01-01','2011-12-31');
Courtesy of https://stackoverflow.com/a/10132142/375087

sum hits for every week in mysql

I have a table with number of page views per day. Something like this:
+------+------------+------+----------+
| id | date | hits | mangaID |
+------+------------+------+----------+
| 4876 | 1331843400 | 132 | 13 |
+------+------------+------+----------+
| 4876 | 1331929800 | 24 | 236 |
+------+------------+------+----------+
| 7653 | 1331929800 | 324 | 13 |
+------+------------+------+----------+
I'm trying to get sum hits from last week with the below code:
SELECT sum(hits) as hits FROM om_manga_views WHERE DATE_SUB(CURDATE(),INTERVAL 1 week) <= date and mangaID = '13'
My problem is that I'm storing date as time using strtotime in date's field as int type.
So how can i get what i want!?
Try this:
select sum(hits) hitCount from t
where from_unixtime(date) >= current_date() - interval 1 week and mangaId = 11
Here is the fiddle to play with.
I slightly changed your data because the records you provided are older than 7 days, so the sum would return 0.

Categories