edit: I have a table with start and end time fields. I would group the intervals between the common hours. It's possible?
Assume this table:
time_begin time_end
07:00 18:00
09:00 12:00 (this interval is in the first line)
17:00 18:00 (this interval is in the first line)
I would like to return the lines:
time_begin time_end
09:00 12:00
17:00 18:00
Thanks
Try this query.
SELECT time_begin, time_end
FROM table_name
WHERE time_begin < (SELECT time_end
FROM table_name
LIMIT 1)
AND time_begin > (SELECT time_begin
FROM table_name
LIMIT 1)
AND time_end < (SELECT time_end
FROM table_name
LIMIT 1)
AND time_end > (SELECT time_begin
FROM table_name
LIMIT 1)
This query returns all time intervals which are between the time interval in the first row.
Related
I have data like this
sno name date time
1 Irona 2016-01-01 10:00:00
2 Meona 2016-01-02 21:00:00
3 Lessa 2016-01-02 8:00:00
4 Airik 2016-01-03 10:00:00
I m trying query like this
SELECT * FROM `appointment` where (date <= '2016-01-02' and time >= '21:00:00') and (date >= '2016-01-03' and time >= '10:00:00')
I want those appointment which are between 2016-01-02 and 2016-01-03 and also between 9 pm to 10 am
Try this:
SELECT *
FROM `appointment` A
WHERE STR_TO_DATE(CONCAT(A.date, ' ', A.time), '%Y-%m-%d %H:%i:%s') BETWEEN '2016-01-02 21:00:00' AND '2016-01-03 10:00:00'
Query date <= '2016-01-02' and date >= '2016-01-03' returns nothing.
you should change it to date >= '2016-01-02' and date <= '2016-01-03'
SELECT * FROM `appointment`
where date >= '2016-01-02' and date <= '2016-01-03'
and time <= '21:00:00' and time >= '10:00:00'
I will do it like this:
...
WHERE
to_char(date, '%Y%m%d') between '20160102' and '20160103'
AND to_char(timedate, '%H') between '10' and '21'
I need to make statistics page for billing with auto query. For example now is july and query must show count of record for the june and current count of records on current day(only july). Sort of:
"records for the last month - 85, for the current day - 32"
I have table customer and row create_time but it is in unix timestamp. Tried
SELECT COUNT(*) FROM customer WHERE create_time >=
UNIX_TIMESTAMP(DATE_SUB(now(), INTERVAL 1 MONTH))
but its absolutely not what i want.
I would appreciate for any help.
Try this:
SELECT *
FROM ( SELECT COUNT(*) AS total_previous_month
FROM customer
WHERE create_time >= concat(date_format(LAST_DAY(now() - interval 1 month),'%Y-%m-'),'01')
AND create_time < LAST_DAY(now() - interval 1 month )
) AS s1,
( SELECT COUNT(*) AS total_previous_month
FROM customer
WHERE create_time >= concat(extract(year from now()),'-',extract(month from now()),'-01')
AND create_time <= now()
) AS s2
You can do this by adding condition to the WHERE:
create_time >= UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 1 month))
in PHP you can calculate the one month back date
$newDate = strtotime('-1 month');
and use this in query
I have a mysql DB that has a TIMESTAMP field titled date. How can I select all fields where the last month ?
Forexample 01-05-2014 then 31-05-2014 etc..
Thanks in advance!
select * from table where timestamp_col=LAST_DAY(timestamp_col)
Source
As per my understanding do you want this like...
SELECT * FROM table_name where timestamp_col BETWEEN '2014-05-01' AND '2014-05-31';
SELECT DATETİME, FROM_UNIXTIME(datetime) TİMESTAMP
FROM table_name
WHERE DATETİME >= UNIX_TIMESTAMP( DATE_ADD(LAST_DAY(NOW() + INTERVAL 1 DAY - INTERVAL 2 MONTH) , INTERVAL 1 DAY ))
AND DATETİME < UNIX_TIMESTAMP( (LAST_DAY(NOW() + INTERVAL 1 DAY - INTERVAL 1 MONTH)) )
I have a simple table with bunch of applicants, where they have their Start Time, End Time and Date data. I want to figure out who is available for a specific date within a Time Range.
Table below shows where Start and End Date Columns tells us they are booked for that Date/Time .........
AppID StartTime EndTime Date
H12 8:00 13:00 12/1/2013
H12 14:00 16:00 12/1/2013
H12 19:00 21:00 12/1/2013
H14 17:00 18:00 12/1/2013
H13 14:00 16:00 12/1/2013
H13 11:00 15:00 12/2/2013
H15 8:00 13:00 12/2/2013
So in the Table above how can I write a Query that will say...Give me all the applications for 12/1/2013 which are NOT working between 17:00 - 18:00? So basically it should return H12 & H13 (Because its time slot for 17-18 pm is not available within table for 12/1/2013).
This query returns the apps ID which were not working at a time range on a specific date.
SELECT
[AppID] = FreeApps
FROM table_name t1
WHERE AppID NOT IN (-- Not in the set of apps that were busy at the time range
SELECT AppID
FROM table_name t2
WHERE ((StartTime >= '17:00' AND StartTime <= '18:00')
OR (EndTime >= '17:00' AND EndTime <= '18:00'))
AND Date = '12/1/2013'
AND t1.AppID=t2.AppID)
GROUP BY AppID
;WITH t1 as
(
SELECT DISTINCT AppID
FROM <table>
WHERE date = '20130112'
)
SELECT AppID
FROM t1
WHERE
NOT EXISTS
(SELECT * FROM <table> t2
WHERE t2.STARTTIME < '18:00'
AND t2.ENDTIME > '17:00'
AND t1.AppID = t2.AppID
AND t2.date = '20130112'
)
I am have a column of dates that I need to add a variable amount of days to. So I don't think I can use the normal date_add (date, interval x day).
date numOfDays
2001-01-01 5
2001-05-22 3
2002-03-04 2
... ...
I'm basically looking for something like:
select date, numOfDays, (date + numOfDays) as newDate
from t1;
SELECT `date`,
`numOfDays`,
(`date` + INTERVAL `numOfDays` DAY) AS `newDate`
FROM `t1`;