Check time between two columns in MySQL - php

I have a MySQL table, booking:
id shop_id date start_time end_time
1 21 2019-1-13 09:00 10:00
2 21 2019-1-13 11:00 11:30
Now before I a insert record into booking table I want to check whether date and time is already booked or not.
How can I do this?
I tried with the following code:
SELECT * FROM booking
WHERE CAST(start_time as date) BETWEEN
'2012-08-30' AND '2012-08-31';

If I understand correctly, you'll have separate values for #date, #start_time, and #end_time that you want to check in the table.
To return all overlaps:
SELECT b.*
FROM booking b
WHERE #date = b.date AND
#end_time >= b.start_time AND
#start_time < b.end_time;

As per your table structure it is better to use
SELECT * FROM booking
WHERE CAST(CONCAT(date,' ',start_time)AS timestamp)
BETWEEN '2012-08-30' AND '2012-08-31';
Otherwise it is better to use full timestamp for both start_time and end_time.

Related

Retrieve records that have expired using the where clause in MySQL and different columns by date and time

Here is my current mysql table
id
Name
expiry_date
expiry_time
1
Sample1
09-01-2023
11:00 AM
2
Sample2
09-01-2023
3:00 PM
3
Sample3
08-01-2023
10:00 PM
Query:
select *
from table
where STR_TO_DATE(table.expiry_date, '%d-%m-%Y') < '2023-01-09'
OR (DATE_FORMAT(concat(table.expiry_date,' ',table.expiry_time),'%d-%m-%Y %g:%i %A') <= NOW()))
I also wish to obtain the records that are out-of-date as of the present time.
How to do that?
You can use STR_TO_DATE to convert your varchar date as follows :
select *
from _table
WHERE STR_TO_DATE(concat(_table.expiry_date,' ',_table.expiry_time), '%d-%m-%Y %h:%i %p') <= now();
you can check it here : https://dbfiddle.uk/emR4cg3s

Fetching sum of amount for a specific year from mysql

I have the following table with dummy values in mysql database:
id cnic amount depositDate receivedBy receivingZone remarks
1 11111 10000 01-Nov-2019 11111 1 Ok
2 11111 10000 07-Nov-2019 11111 1 ok
Now i want to get the sum of amount from the table for specific year (2019 in this case) where the year came from current timestamp (it may be 2020, 2021 etc depends on the current date)
Any help plz
You can use the YEAR function to get the year of the depositDate column and also the current year and then sum only the values which match:
SELECT SUM(amount) AS amount
FROM yourtable
WHERE YEAR(STR_TO_DATE(depositDate, '%d-%b-%Y')) = YEAR(CURDATE())
You can try below -
select sum(amount)
from tablename
where year(depositdate)=year(now())
I would write the WHERE clause to be sargable:
SELECT SUM(amount)
FROM yourTable
WHERE depositDate >= DATE_FORMAT(NOW() ,'%Y-01-01') AND
depositDate < DATE_FORMAT(DATE_ADD(NOW(), INTERVAL 1 YEAR) ,'%Y-01-01');
This approach, while a bit more verbose than the other answers which use the YEAR() function, would allow an index on the depositDate column to be used.
Based on your sample year, we need to recognize first the date using str_to_date
select sum(amount)
from tableA
where year(now()) = year(str_to_date(depositdate, '%d-%b-%Y'))

Search record between 3 date ranges

I need to output data from a DB table that selects records between 3 (not 2) date/time ranges
E.g. start time : 2019-09-07 18.00
end time : 2019-09-07 20.00
so the user should be able to see the record 25 minutes before the start date-time (6.p.m - 18.00), during the event but not after the end date-time (8.p.m -20.00).
I've tried
db->query = "SELECT o_id, schedule, date, start_time, end_time FROM working_schedule WHERE o_id = '".$user_id."'
AND (start_time <= '".date('Y-m-d\TH:i:s', strtotime("-25 minutes"))."' AND start_time >= '".date('Y-m-d\TH:i:s')."')
AND end_time >= '".date('Y-m-d\TH:i:s')."'";
but the result is NULL.
For reference HERE'S a sql fiddle.
Thanks in advance for pointing me in the right direction.
Do you need this ?
select * from working_schedule
where
NOW() BETWEEN DATE_SUB(start_time,INTERVAL 25 MINUTE) AND end_time

Select date with n days from the date added

I have a mysql table with the following fields
Name | Email | Date | Status
I want to extract the records where date range is between 30 days
Assume today is 2014/12/9
ie. date values are
2014/11/25
2014/12/2
2014/12/1
2014/10/25
2014/11/9
I need the o/p as (the number of days should be with in 30 days from the db date to today date)
2014/11/25
2014/12/2
2014/12/1
2014/11/9
I want to extract records those have the interval of less than 30 days from the date in the db.
Yes. I want to fetch the record between 2 days. For this I used this query
SELECT * FROM tbl_jobboard WHERE dtDate <= ( dtDate +30 )
But it is not working.
How to write the select query?
USE DATE_SUB like this:
SELECT * FROM table1
WHERE `date` BETWEEN DATE_SUB(CURDATE(),INTERVAL 30 DAY) AND CURDATE()
Working Fiddle Demo: http://sqlfiddle.com/#!2/6344f2/1
use following query
select * from table_Name t where t.date<=now() and t.date>=DATE_SUB(now(),
INTERVAL 31 DAY)

MySQL BETWEEN DATE RANGE

I have a scenario where I need to pull up delivery dates based on a table below (Example)
job_id | delivery_date
1 | 2013-01-12
2 | 2013-01-25
3 | 2013-02-15
What I'm trying to do is show the user all the delivery dates that start with the earliest (in this case it would be 2013-01-12) and add an another 21 days to that. Basically, the output I would expect it to show of course, the earliest date being the starting date 2013-01-12 and 2013-01-25. The dates past the February date are of no importance since they're not in my 21 date range. If it were a 5 day range, for example, then of course 2013-01-25 would not be included and only the earliest date would appear.
Here is main SQL clause I have which only shows jobs starting this year forward:
SELECT date, delivery_date
FROM `job_sheet`
WHERE print_status IS NULL
AND job_sheet.date>'2013-01-01'
Is it possible to accomplish this with 1 SQL query, or must I go with a mix of PHP as well?
You can use the following:
select *
from job_sheet
where print_status IS NULL
and delivery_date >= (select min(delivery_date)
from job_sheet)
and delivery_date <= (select date_add(min(delivery_date), interval 21 day)
from job_sheet)
See SQL Fiddle with Demo
If you are worried about the dates not being correct, if you use a query then it might be best to pass in the start date to your query, then add 21 days to get the end date. Similar to this:
set #a='2013-01-01';
select *
from job_sheet
where delivery_date >= #a
and delivery_date <= date_add(#a, interval 21 day)
See SQL Fiddle with Demo
SELECT date,
delivery_date
FROM job_sheet
WHERE print_status IS NULL
AND job_sheet.date BETWEEN (SELECT MIN(date) FROM job_sheet) AND
(SELECT MIN(date) FROM job_sheet) + INTERVAL 21 DAY
SELECT j.job_id
, j.delivery_date
FROM `job_sheet` j
JOIN ( SELECT MIN(d.delivery_date) AS earliest_date
FROM `job_sheet` d
WHERE d.delivery_date >= '2013-01-01'
) e
ON j.delivery_date >= e.earliest_date
AND j.delivery_date < DATE_ADD(e.earliest_date, INTERVAL 22 DAY)
AND j.print_status IS NULL
ORDER BY j.delivery_date
(The original query has a predicate on job_sheet.date; the query above references the d.delivery_date... change that if it is supposed to be referencing the date column instaed.)
If the intent is to only show delivery_date values from today forward, then change the literal '2013-01-01' to an expression that returns the current date, e.g. DATE(NOW())

Categories