I am having some issues with the following query, the issue is that I need it so it displays the courses for the current day until the end of the day not just till the start of the day like it does currently. Basically users cannot access the course if they are trying to access the course on its enddate so i need to some how make it so that they can still access it for 23 hrs 59 mnutes and 59 seconds after the end date I think I have to add some sort of time to the NOW() to accomplish this but im not sure how to go about this.The query is as follows:
if ($courses = $db->qarray("
SELECT `CourseCode` AS 'code' FROM `WorkshopUserSessions`
LEFT JOIN `WorkshopSession` ON (`WorkshopUserSessions`.`sessionid` = `WorkshopSession`.`id`)
LEFT JOIN `WorkshopCourses` ON (`WorkshopSession`.`cid` = `WorkshopCourses`.`cid`)
WHERE `WorkshopUserSessions`.`userid` = {$info['uid']} AND `WorkshopUserSessions`.`begindate` <= NOW() AND `WorkshopUserSessions`.`enddate` >= NOW()
ORDER BY `WorkshopUserSessions`.`begindate` ASC
")) {
Any help would greatly be aprreciated!
Thanks,
Cam
I think you need to modify it like this
enddate + INTERVAL 1 DAY >= NOW()
Ofcourse this adds 24 hours, for 23:59:59 just change >= to >
It sounds like you just need to use date_add
DATE_ADD(`WorkshopUserSessions`.`enddate`, INTERVAL 1 DAY) > NOW()
Try replacing:
AND `WorkshopUserSessions`.`enddate` >= NOW()
with
AND DATE(`WorkshopUserSessions`.`enddate`) = CURDATE()
Hope it helps.
Related
Hello there I have a question so I have a goal mysqli query and it sum the price and select by interval 1 month I try few things to made it to select only from 1st day of the month but I have no idea anymore how to do it..
So here is my code
$monthquery=mysqli_query($link,"SELECT SUM(price) FROM `transactions` WHERE `date` BETWEEN date_sub(CAST(CURRENT_TIMESTAMP AS DATE),INTERVAL 1 MONTH) AND CAST(CURRENT_TIMESTAMP AS DATE);")->fetch_assoc();
$month = $monthquery["SUM(price)"];
$percent = 0;
$percent = ($month*100)/$donation_goal;
$goal = round($percent, 2);
I want it to select let say If the customer start from 20/12/2019 and everything have to be reset on 01/01/2020, not like now from 20th to 20th.. I saw few things here on stackoverflow I try them and it does not always it give me same result.
Thanks anyone.
select only from 1st day of the month
You can do:
`date` >= DATE_FORMAT(NOW() ,'%Y-%m-01')
I have a mysql table orders in that I have a column order_date which is current time stamp(2016-08-17 00:00:00.000000). now I want to select or count the data's entered this month and the previous month, after this I can find the difference between these two months I am using this code and it is not working.
$sql="SELECT * FROM order WHERE order_date > DATE_SUB(NOW(), INTERVAL 1 MONTH)";
$result = $this->db->query($sql);
return $result;
this is not working an mysql error is produced.
Try
$sql="SELECT * FROM order WHERE DATE(order_date) LIKE DATE_SUB(CURDATE(), INTERVAL 1 MONTH)";
$result = $this->db->query($sql);
return $result;
i think this Link[http://sqlhints.com/2015/07/10/how-to-get-difference-between-two-dates-in-years-months-and-days-in-sql-server/] will help you
Use this. Hope it helps what you want. Thanks
$todayDate = date('Y-m-d');
$todayMonth = date("m", strtotime($todayDate ));
$previousMonth = $todayMonth - 1;
$sql = "SELECT * FROM order WHERE MONTH(order_date) BETWEEN '$todayMonth' AND '$previousMonth'";
First, the following is the correct logic to get all values from the current month and all of the previous month:
select *
from orders o
where order_date >= date_sub(date_sub(curdate(), interval day(curdate) - 1 day), interval 1 month);
Then, use conditional aggregation for comparison. Here is an easy way:
select sum(month(order_date) = month(curdate())) as cur_month,
sum(month(order_date) <> month(curdate())) as prev_month,
(sum(month(order_date) = month(curdate())) -
sum(month(order_date) <> month(curdate()))
) as diff
from orders o
where order_date >= date_sub(date_sub(curdate(), interval day(curdate) - 1 day), interval 1 month);
Note: I don't fully see the utility of comparing a partial month (this month) to a full month (last month), but that is what you seem to be asking for. If you are asking for something different, then ask another question with sample data and desired results.
Hi there :) I am doing a query, and it will pull results where the timestamp is more than 30 days old. Here's what I've put so far:
$result = $db->query("SELECT time FROM table1 WHERE open_time < (NOW() - INTERVAL 30 DAYS)");
So as you can see, it will try to find results more than 30 days old, however when I execute the query, it doesn't select any rows, even though the column field "open_time" is more than 30 days old.
Any suggestions I do greatly appreciate :)
Have you tried to cast the date field?
$result = $db->query("SELECT time FROM table1 WHERE CAST(open_time AS DATE) < (NOW() - INTERVAL 30 DAYS)");
EDIT:
Maybe you should use this tricky query:
$result = $db->query("SELECT time FROM table1 WHERE open_time <
UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 DAY))"):
Try this Query
$result = $db->query("SELECT time FROM table1 WHERE DATE(`date`) = DATE(NOW() - INTERVAL 30
DAY")
or
try this one also for your reference
timestampadd()
SELECT * FROM table WHERE `date` > timestampadd(day, -30, now());
try this link also
I try to get a list of all records that are older than 1year ago from my database, the field for expired_contract has next information.
expired_contract DATE NOT NULL
So it takes the DATE in the next format: YEAR-MM-DD, next i have the sql that i cant get it working sadly.
$sql = "SELECT *
FROM My_Contracte
WHERE expired_contract >= DATE_SUB(NOW(),INTERVAL 1 YEAR)
ORDER BY id_contract DESC";
I tried a lot of "WHERE" commands but none worked as i expected. Can you help me get this working? I'm looking on this for about 5hours i need exact command to get it worked.
The $sql gets me something but takes it wrong, i get dates like: 2015-10-01, 2016-10-01 and date like 2014-09-30 doesn't show up.
Basically i want to show dates like:
If today is 2015-10-01 i want to see dates older than 1year ago so from 2014-09-30 and not showing dates like 2015-10-01, 2016-10-01.
Maybe do i have to edit something in database?
Looking for your help, thank you!
You have to use lower than instead of greater or equals:
$sql = "SELECT * FROM My_Contracte WHERE expired_contract < DATE_SUB(NOW(),INTERVAL 1 YEAR)
SELECT SUM(impressions) AS impressions FROM My_Contracte where DATE(expired_contract) BETWEEN DATE_SUB( CURDATE( ) ,INTERVAL 1 YEAR ) AND CURDATE( )
SELECT * FROM My_Contracte WHERE (expired_contract NOT BETWEEN DATE_SUB(CURDATE(),INTERVAL 1 YEAR) AND CURDATE()) and expired_contract<=NOW() ;
I am trying to grab the newest records from my table. I want all of the records that happened in the past 7 days. Here is what I have so far to start with.
$query = "SELECT * FROM mlg_logattempts AS a WHERE a.ts = (SELECT MAX(ts) FROM mlg_logattempts AS b WHERE b.ts > NOW() - INTERVAL 5 MINUTE AND b.name = a.name)";
I have used intervals in the past but an unsure how to make this work now. Can someone show me the proper way to request the past 7 days records? I do have a timestamp field.
UPDATE
Unfortunately I realized the command I shared with you. I do not have any of the above fields. The only date field I have is "date". no a or ts.
You could use mysql date_diff() for dates
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_datediff
but since you use timestamps, the interval is a good solution:
b.ts > unix_timestamp(CURDATE()-INTERVAL 7 DAY)
Supposing that the date of login attempt is b.ts and it's formatted like 2013-08-20 03:08:
$past7days = date("Y-m-d H:i:s",strtotime("-7day"));
$query = "SELECT * FROM mlg_logattempts AS a WHERE a.ts = (SELECT MAX(ts) FROM mlg_logattempts AS b WHERE date >= '$past7days' AND b.name = a.name)";