select query for time interval in mysql and php - php

select query to select all records that were inserted in 15 minutes.for ex if now time is 10:00 then it shoulld fetch all record inserted from 9:45 to 10:00

Assuming you have a DATETIME column named created in your table:
SELECT id FROM tablename
WHERE ((created > DATE_SUB(NOW(), INTERVAL 15 MINUTE))
AND (created < NOW()))

select * from table WHERE datetimeField BETWEEN date_add(NOW(),INTERVAL -15 MINUTE) AND NOW();

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

Mysql query not working with IST timezone

I have table "bookings" in mysql and i want to fetch those record whose "bookingtime"(data type is "Datetime") more than 40 minutes ( according to IST)
My query not working with IST,
Here is my table "booking"
id name bookingtime
1 ayhd 2020-01-09 15:23:33
2 dhdye 2020-01-09 14:53:38
3 juey 2020-01-09 11:13:53
I tried with following query but not showing any result,Where i am wrong ?
SELECT * FROM booking
WHERE bookingtime < (NOW() - INTERVAL 40 MINUTE)
try this query , it will get records between now and 40 minutes ago
SELECT * FROM `booking` WHERE `bookingtime` >= (NOW() - INTERVAL 40 MINUTE) AND `bookingtime` < NOW()

MYSQL Last month Query is not returning first 9 days

I am querying records from the last calendar month. As it is February, it should return all the records that were added on January this year.
My Query:
`SELECT * FROM table_name WHERE date >=
DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 2 MONTH)), INTERVAL 1
DAY) AND date <= DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 1
MONTH)), INTERVAL 0 DAY) AND campaign = '$campaign' ORDER BY date
ASC`
It returns some records but skips the first 9 days. It starts showing records from 10th of the previous month. What am I missing here?
check your date field type and make sure you have not mistaken it with varchar.
SELECT * FROM table_name WHERE
(MONTH(date) = (MONTH(NOW()) - 1) AND YEAR(date) = YEAR(NOW()))
OR
(MONTH(date) = 12 AND MONTH(NOW())=1 AND YEAR(date) = (YEAR(NOW()) - 1) AND campaign = '$campaign' ORDER BY date
ASC`
Try To Get Data in step by step like,
First, you should try below query.
SELECT Date, DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 2 MONTH)), INTERVAL 1 DAY) AS StartDate, DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 1 MONTH)), INTERVAL 0 DAY) AS EndDate FROM MyTable
Second, If First Step give right date then get your data by directly writing your date rather than DATE_ADD function in where clause.
Third, If These will Give you write DATA then try to fetch data using DATE_ADD function.
Replay If you will get solution.
SELECT * FROM table_name WHERE date between DATE_SUB(DATE_SUB(CURRENT_DATE,INTERVAL DAYOFMONTH(CURRENT_DATE)-1 DAY),INTERVAL 1 MONTH) AND DATE_SUB(DATE_SUB(CURRENT_DATE,INTERVAL DAYOFMONTH(CURRENT_DATE)-1 DAY),INTERVAL 1 DAY) AND campaign = '$campaign' ORDER BY date ASC

MySQL retrieve data for the past 24 hours when the column table is of type date NOT timestamp

I am trying to retrieve data for the passed 24 hours but I am getting nothing, but when I check on the table column (days) directly in db, I can find them. Here is my query:
select * from mytable
where
days between concat(date(date_sub(now(), interval 1 day))
between requires 2 arguments that specify the range:
select * from mytable
where days between date_sub(now(), interval 1 day) and now();
Or simply
select * from mytable
where days > date_sub(now(), interval 1 day);
You can use
select * from mytable where days > date_sub(CURDATE(),INTERVAL 1 DAY);

query to select data from DB based on a particular day and count how many rows are there?

I have Db which has data of an year...so I need a query to select data from based on a date(particularly yesterday's) and count how many are in this.....
Help please!
It has a column with name "created" where all the date & time are present.
code used:
//$thirty_reg = mysql_query("SELECT column FROM user WHERE user.date BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW()");
//$num_thirty=mysql_num_rows($thirty_reg);
//echo $num_thirty;
select *
from YourTable
where date_sub(curdate(), interval 1 day) < DateColumn
and DateColumn < curdate()
To fetch result set:
SELECT * FROM table WHERE data_date = DATE_SUB(NOW(), INTERVAL 1 DAY)
To fetch count of rows:
SELECT COUNT(*) FROM table WHERE data_date = DATE_SUB(NOW(), INTERVAL 1 DAY)

Categories