id name quantity timestamp
1 item1 2 2015-06-01 20:00:00
2 item2 5 2015-06-01 22:30:00
3 item3 2 2015-06-02 20:00:00
4 item4 7 2015-06-02 20:30:00
5 item5 9 2015-06-02 21:30:00
This is an example database, 'timestamp' is in datetime format and contains varying values. Note that the table contains various date and time data. How do i get all the rows of one day and of certain minute(above eg: 30) only, eg how can i select all the rows with timestamp 2015-06-01 **:30:00, here ** = hour any help, thanks.
You can use DATE & MINUTE functions -
SELECT * from your_table WHERE DATE(`timestamp`) = '2015-06-02' AND MINUTE(`timestamp`) = 30
You need to write follow query:
SELECT * FROM Your_Table_Name WHERE DATE(timestamp) = '2015-06-01' AND TIME(timestamp) LIKE '%:30:00'
Related
User input = '2017-03-12'
Let say I have this tableRevenue
date revenue
---------- ---------
2017-01-01 100
2017-01-08 100
2017-01-15 100
2017-01-22 100
2017-01-29 100
2017-01-05 100
2017-01-12 100
2017-02-19 100
2017-02-26 100
2017-03-05 100
2017-03-12 100
And another tableHolidays which contains
date
----------
2017-01-15
2017-02-19
2017-03-05
I want to display it like this:
date revenue
---------- ---------
2017-01-01 100
2017-01-08 100
2017-01-22 100
2017-01-29 100
2017-01-05 100
2017-01-12 100
2017-02-26 100
2017-03-12 100
I want to display the revenue each of the last 8 weeks and I want to skip all the dates that are existing in tableHolidays using a loop. Is this possible in PHP?
mention: you didn't tag any specific database - my answer will refer to SQL-Server:
assuming #UserDate is a variable with the user input date
Use Date-Functions (specific to every DB system) to calculate the date range. in your case to subtract the 8 weeks.
Select all rows within this date range
exclude (NOT IN) all dates from your resultset which occur in your tableHolidays table
GROUP BY weeks (calculate weeknumber with WEEK) and SUM the revenue
Query:
SELECT WEEK(tR.date) as weeknr
,SUM(tR.revenue)
FROM tableRevenue tR
WHERE tR.date >= DATEADD(wk,-8,#UserDate)
AND tR.date <= #UserDate
AND tR.date NOT IN (SELECT date FROM tableHolidays)
GROUP BY WEEK(tR.date)
you can use the 'ANY' which is a mysql operator
for more information you can visit this link
https://www.w3schools.com/sql/sql_any_all.asp
$userInput = '2017-03-12';
$sql = "SELECT `date`, `revenue`
FROM `tableRevenue`
WHERE (
(`date` = ANY
(SELECT `date` FROM `tableHolidays` WHERE DATE(date)<='{$userInput}'))
AND (DATE(date) <='{$userInput}')
)";
I have a table "bookings" that looks like
bookings
id id_class date day name
1 2 2016-03-10 16:00:00 monday Fredrik
2 5 2016-03-11 16:00:00 tuesday Richard
3 7 2016-03-11 18:00:00 tuesday Sara
4 4 2016-03-11 15:00:00 tuesday Fredrik
Then I extract that table using php
<?php
$result = $con->query("select * from bookings");
<?
What I want now is a php function that tells me how many rows that contain a specific entry such as "id_class=2" or how many rows contain (name=Fredrik) where the answer should be between 0 and a positive number. Is there a way for this?
Use where to get specific condition
SELECT COUNT(*) FROM bookings WHERE id_class=2
SELECT COUNT(*) FROM bookings WHERE name='Fredrik'
I a have table in mysql with some data like this:
id from to
---- -------------------- ---------------------------
1 2013-01-31 23:50:00 2013-02-02 09:00:00
2 2013-02-05 11:21:12 2013-02-08 01:01:01
3 2013-02-08 17:33:44 2013-02-08 18:22:55
4 2013-02-12 01:40:12 2013-02-12 02:00:59
5 2013-02-28 01:40:12 2013-03-02 02:00:59
I need a Mysql query or a php code for finding difference between 'from' column and 'to' column of each rows, and find how many seconds are there in each day between 'from' and 'to' date separately, for example for row 1 the needed output be something like this:
difference between 2013-01-31 23:50:00 - 2013-02-02 09:00:00 for row 1
2013-01-31 : 600 sec (24:00:00 - 23:50:00 => 600 sec)
2013-02-01 : 86400 sec (24:00:00 - 00:00:00 => 86400 sec)
2013-02-02 : 32400 sec (09:00:00 - 00:00:00 => 32400 sec)
and so on ... for each row
MySQL command is preferred. which code can create this output? is there any specific function in php or mysql for creating this output?
I read these answers:
mysql calculate seconds between two date/times for each day
MySQL: how to get the difference between two timestamps in seconds
but these are not my answer.
for information:
link 1 is a question like my question but the answer is not true because "The command needs grouped by day" but how?!
and the second is not my question but question one marked as duplicated with link 2 and it is not true.
Let me assume that you have a table of dates. If so, you can use a join or correlated subquery:
select d.date,
(select sum(timestampdiff(second,
greatest(d.date, t.from),
least(d.date, t.to)
))
from table t
where t.to >= d.date and
t.from < date_add(d.date, interval 1 day)
) as NumSeconds
from dates d;
If you don't have a dates table of some sort, you can create one on the fly:
from (select date('2013-01-31') as date union all
select date('2013-02-01') union all
. . .
) dates
Going to make a line graph. I want to query count with difference of 5 days.
Suppose if I have following rows:
booking_date
2015-02-1
2015-02-3
2015-02-5
2015-02-6
2015-02-6
2015-02-9
2015-02-10
2015-02-15
2015-02-17
2015-02-23
2015-02-28
In above table column it contains date. Now How can I do mysql query so that it can return with difference of 5 days like:
1 => 3 // count of date between 2015-02-1 & 2015-02-05 is 3
2 => 4 // count of date between 2015-02-06 & 2015-02-10 is 4
3 => 1 // count of date between 2015-02-11 & 2015-02-15 is 1
4 => 1 // count of date between 2015-02-16 & 2015-02-20 is 1
5 => 1 // count of date between 2015-02-21 & 2015-02-25 is 1
6 => 1 // count of date between 2015-02-26 & 2015-02-30 is 1
Is any direct way to query like above. I am not so good at mysql. But can do php nicely.
You can do the following to get everything in the same query.
First of all get the unix timestamp for the date you want to start grouping in 5 days. In your example that would be 2015-02-01 -> 1422748800
Then the query would be the following:
SELECT COUNT(*), FLOOR((UNIX_TIMESTAMP(booking_date) - 1422748800)/ (60*60*24*5)) as FiveDayPackNumber from tbl GROUP BY FLOOR((UNIX_TIMESTAMP(booking_date) - 1422748800)/ (60*60*24*5))
Haven't tested it so it may require some tweaking but you can get the idea: It will group them by the number of 5-days-packs that passed since your initial date, starting at 0.
Do you mean to do something like:
SELECT COUNT(1) FROM {table_name} WHERE booking_date > 2015-02-01 AND booking_date < 2015-02-05
?
(completely off memory so you might need to slightly alter it)
SELECT count(*)
FROM tbl
WHERE booking_date >= start_date
AND booking_date <= end_date
This is what I need the update to do:
update my summary table TIME column with the user-edited TIME value
compare the new TIME, using ID's from my summary table and hour_interval table to join tables
find the correct row ID where the new TIME falls within (there are two columns with start and end hours; example would be: start column - 04:00:00 end column 06:00:00)
update the summary table with the correct interval row ID using the summary table ID
The code executes, but doesn't update the interval ID. Suggestions?
UPDATE summary S
JOIN hour_interval H
ON S.hourinterval_id = H.hourinterval_id
SET S.hourinterval_id = H.hourinterval_id
WHERE ('$new_time' BETWEEN H.start_hour AND H.end_hour)
AND summary_id = '$summary_id'"
hourinterval_id start_hour end_hour
1 4:00:00 5:59:59
2 6:00:00 7:59:59
3 8:00:00 9:59:59
4 10:00:00 11:59:59
5 12:00:00 13:59:59
6 14:00:00 15:59:59
7 16:00:00 17:59:59
8 18:00:00 19:59:59
Try this:
UPDATE summary S
JOIN hour_interval H
ON '$new_time' BETWEEN H.start_hour AND H.end_hour
SET S.hourinterval_id = H.hourinterval_id
WHERE summary_id = '$summary_id'"