This is my query.
SELECT *
FROM `mystores`
WHERE `status`='0'
AND DATE(`create_date`) < '2014-10-23'
AND DATE(`create_date`) > '2014-10-17'
and it shows all the records between 2014-10-18 and 2014-10-22.
Now I want only 2014-10-18, 2014-10-20 and 2014-10-22 date's records.
If I run this query tomorrow, it should show 2014-10-19, 2014-10-21 and 2014-10-23
and My PHP code:
$sql="SELECT * FROM `mystores` WHERE `status`='0' AND DATE(`create_date`) < '".date("Y-m-d", strtotime("-1 day"))."' AND DATE(`create_date`) > '".date("Y-m-d", strtotime("-7 days"))."'";
Is there any possible to get these records?
Thanks
SELECT *
FROM `mystores`
WHERE `status`='0'
AND DATE(`create_date`) in (curdate() - interval 2 day,
curdate() - interval 4 day,
curdate() - interval 6 day)
Related
I have a Table in Database which has records of Logins.
Table name: user_logins
ID | timestamp
1 2019.01.03 (Year, Month, Day)
2 2019.01.04
3 2019.01.05
4 2019.01.05
5 2019.01.07
6 2019.01.07
7 2019.01.09
I want to Show only Count of Records by this Week.
From Monday to Sunday (04-02-2019 ... 10-02-2019)
My PHP and SQL Code is:
$mo = mysql_num_rows(mysql_query('SELECT * FROM user_logins WHERE DAYNAME(DATE(timestamp)) = "monday" and timestamp >= DATE_SUB(CURDATE(), INTERVAL DAYOFWEEK(CURDATE())-0 DAY)'));
this should show the records of 04-02-2019
Here is my SQL Fiddle link:
SQL Fiddle
This:
DATE_ADD(CURDATE(), INTERVAL - WEEKDAY(CURDATE()) DAY)
gives this week's Monday.
So:
SELECT * FROM user_logins
WHERE
timestamp
BETWEEN DATE_ADD(CURDATE(), INTERVAL - WEEKDAY(CURDATE()) DAY)
and
NOW()
Try following query:
SELECT id FROM `user_logins`
WHERE timestamp >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY
AND timestamp < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY
Demo
In my DB I have a column called dato_tid (Datatype = date)
right now I have 2 post
1 where the date is 2018-07-18
2 where the date is 2018-07-20
I need to select the post, that has less then 24 hours to go
SELECT * FROM `udflyt` WHERE dato_tid > DATE_ADD(CURDATE(), INTERVAL -1 day)
this line will select both posts
SELECT * FROM `udflyt` WHERE dato_tid > DATE_ADD(CURDATE(), INTERVAL -24 HOUR)
and so will this, I did try to change the > to < but the same.
SELECT * FROM `udflyt` WHERE dato_tid > (now() - interval 1 day )
this line will also get both posts
So what do I need to do, Thanks
Actually, 24 hours to go means you should ADD a day, not subtract a day.
SELECT *
FROM `udflyt`
WHERE dato_tid <= DATE_ADD(CURDATE(), INTERVAL +1 day);
Will provide only the record with date '2018-07-18' (which is what you are looking for, I believe.
The below shows the values used for comparison for both doing addition and subtraction.
SELECT *, DATE_ADD(CURDATE(), INTERVAL +1 day), DATE_ADD(CURDATE(), INTERVAL -1 day)
FROM `udflyt`
WHERE dato_tid <= DATE_ADD(CURDATE(), INTERVAL +1 day);
I have a table:
I want to select data with res_time within the past 15 minutes. I am using:
SELECT * FROM test WHERE res_time >= DATE_SUB(now(), INTERVAL 15 MINUTE).
SELECT * FROM `test` WHERE res_time between timestamp(DATE_SUB(NOW(), INTERVAL 15 MINUTE)) AND timestamp(NOW()).
If you are using PHP then you can do this
$beforeTime = date("Y-m-d h:i:s",strtotime("-15 minutes"));
$query = "select * from test where res_time >= '$beforeTime' " ;
If you want the pure SQL solution then follow other mentioned solutions.
Cheers !!!
Try this if you are not using unix timestamp:
SELECT * FROM test WHERE res_time >= DATE_SUB(CURDATE(), INTERVAL 15 MINUTE);
or
SELECT * FROM test WHERE FROM_UNIXTIME(res_time) >= DATE_SUB(CURDATE(), INTERVAL 15 MINUTE)
You could try below query.
SELECT * FROM test WHERE res_time >= DATE_SUB(CURDATE(),INTERVAL 15 MINUTE)
I want to get all the records from mysql database who have updated their records within 30 days from the current date for that i have used the below query but it is not working properly. $tda is the current date and $prevmonth is the date of exactly
30 days back from the current date. Please help. Thanks.
$da=date('d');
$tda=date('d-m-Y');
$prevmonth = date(''.$da.'-m-Y', strtotime('-1 months'));
$sql_q=executeQuery("select * from ".reg." where 'uid' !=".$_SESSION['uid']." AND Updatedate >= '$prevmonth' AND Updatedate <='$tda '");
You can do it in mysql as
`Updatedate` < DATE(NOW() - INTERVAL 30 DAY)
OR
`Updatedate` < DATE_SUB(CURDATE(), INTERVAL 30 DAY)
For Varchar
STR_TO_DATE(Updatedate, '%Y-%m-%d') < DATE_SUB(CURDATE(), INTERVAL 30 DAY)
UPDATE :
The query posted in the comment is wrong and should be
$sql_q=executeQuery("select * from registration
where
`uid` != ".$_SESSION['uid']."
AND STR_TO_DATE(Update_date, '%d-%m-%Y') < DATE_SUB(CURDATE(), INTERVAL 30 DAY)") ;
If you are looking for data within last 30 days then
$sql_q=executeQuery("select * from registration
where
`uid` != ".$_SESSION['uid']."
AND STR_TO_DATE(Update_date, '%d-%m-%Y') >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)") ;
I like doing something like this: AND UpdateDate > NOW() - INTERVAL 30 DAY AND UpdateDate < NOW().
If your Updatedate column is a DATETIME column then you can do the following:
SELECT *
FROM table
WHERE uid <> ?
AND Updatedate >= DATE_SUB(NOW(), INTERVAL 1 MONTH)
AND Updatedate <= NOW();
Or:
SELECT *
FROM table
WHERE uid <> ?
AND Updatedate BETWEEN DATE_SUB(NOW(), INTERVAL 1 MONTH) AND NOW();
Or if it's a timestamp then this:
SELECT *
FROM table
WHERE uid <> ?
AND FROM_UNIXTIME(Updatedate) >= DATE_SUB(NOW(), INTERVAL 1 MONTH)
AND FROM_UNIXTIME(Updatedate) <= NOW();
I want all data from last week.
I used
SELECT id FROM tbl
WHERE date >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY
AND date < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY
but its not working when my week starts with "Monday".
What should I do?
SELECT id FROM tbl WHERE date >= CURDATE() - INTERVAL (WEEKDAY(CURDATE())+7) DAY AND date < CURDATE() - INTERVAL (WEEKDAY(CURDATE())) DAY
I try this and it work for me.
Try
SELECT id FROM tbl
WHERE YEARweek(date) = YEARweek(curdate())
Then change like this
$lastweek = unix_to_human(time("Y-m-d H:i:s") - (7 * 24 * 60 * 60), TRUE, 'us');
SELECT id FROM tbl
WHERE date >= curdate() - $lastweek
AND date < curdate() - $lastweek