Getting mysql result from the last 30 days [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Get from database but only last 30 days
Hi I have some php code which I use to count the rows in a database from the last 30 days. The problem is, that if I change the piece of code so that the number changes from -30 to -20, the output number goes from 272 to 360 instead of going down.
Here is the code:
$result = mysql_query("SELECT * FROM all_count WHERE DATEDIFF(date,NOW()) = -30 and member ='000002'");
$num_rows60 = mysql_num_rows($result);

Try this
select * from `table` where `yourfield` >= DATE_SUB(CURDATE(), INTERVAL 3 MONTH)
For days, year see below for example.
DATE_SUB(CURDATE(), INTERVAL 15 DAY) /*For getting record specific days*/
DATE_SUB(CURDATE(), INTERVAL 1 YEAR) /*for getting records specific years*/
For Anand, query
BETWEEN DATE_SUB( CURDATE( ) ,INTERVAL 6 MONTH ) AND DATE_SUB( CURDATE() ,INTERVAL 3 MONTH )
/* For Getting records between last 6 month to last 3 month

It's better to compare
`date`< DATE(NOW() - INTERVAL 30 DAY)
rather than
DATEDIFF(date,NOW()) = -30
In the first case, the date calculation is done only once, at the beginning of the query, and the database can use any indexes on the date column.
The second query must calculate the DATEDIFF on every row, and the database can't use any indexes. The second query forces a full table scan.
Also, I strongly suggest that you not call your column date. Yes, I know you can quote the name with backticks, but that's just messy and when you forget then your syntax errors will be hard to forget. Come up with a more descriptive name for the column. What kind of date is it? What does the date represent?

You can use this instead:
$result = mysql_query("SELECT * FROM all_count WHERE `date`< DATE(NOW() - INTERVAL 30 DAY) and member ='000002'");

As you can see in the documentation here, the DATEDIFF function in MySQL tells you the difference in days of the first date to the second.
Your query only selects all rows where the difference is exactly 30 days and not those that are up to 30 days ago. So it's completely possible, that the number of rows for the date 20 days ago is higher than 30 days ago. What you most likely wanted was:
SELECT * FROM all_count WHERE DATEDIFF(date,NOW()) >= -30 and member ='000002'

Related

Get Records 30 minutes before date and time

Been trying to get this to work for 2 days and this is frustrating me.
Trying to get records 30 minutes before a date/time (Format in database is datetime).
This is what I have:
select id
from tbl_events
WHERE DATE_SUB(NOW(), INTERVAL -30 MINUTE) = DATE_FORMAT(start, '%Y-%m-%d %H:%i:%s')
What the heck am I missing?
Thanks
You already use the function DATE_SUB() so within that function you can simply use INTERVAL 30 MINUTE without the minus sign.
You also don't have to format start if it is a datetime or timestamp field.
Finally you shouldn't use = because times are hardly every exactly equal.
This gives this query:
select id
from tbl_events
WHERE start < DATE_SUB(NOW(), INTERVAL 30 MINUTE)
Probably. It's not extremely clear what you're trying to do.

mysql select sum of records every 10 minutes past 48 hours

I want to create an array of data with the sum of records in a mysql table.
I need a sum of data from every 10 minutes from now to the last 48hours.
i have a working code in php, but is there a possibility to create this query directly in mysql?
for ($p=0;$i<=2880;$p+10){
$sql = "
select sum(rotations)
from wheel
where time BETWEEN (DATE_ADD(NOW() , INTERVAL - ".$p." MINUTE) AND (DATE_ADD(NOW() ,INTERVAL - ".$p+10." MINUTE)
";
}
Thank you in advance.
You can convert time to a numeric timestamp, then divide by the number of seconds in 10 minutes to group it.
SELECT FROM_UNIXTIME(600 * FLOOR(UNIX_TIMESTAMP(time)/600)) AS start_time, SUM(rotations)
FROM wheel
WHERE time >= DATE_SUB(NOW(), INTERVAL 2 DAY)
GROUP BY start_time

MySQL Date and Interval - records between dates

I've got a syntax problem I can't sort out. I'm just trying to grab all records from last 3 days.
$result = mysqli_query($link,"SELECT * FROM records WHERE today BETWEEN CURRENT_DATE AND DATE_ADD(CURRENT_DATE, INTERVAL 3 DAY)");
today is DB column for the MySQL timestamp and looks like this: 2014-10-30 16:35:58
This query only gives results for 1 day, not 3. Can someone help with the syntax problem?
DATE_ADD(CURRENT_DATE, INTERVAL 3 DAY) means three days in the future, not three days ago. Unless the today column is supposed to represent (say) the date for which a future appointment is scheduled, you usually want to subtract days from a date. So get three days ago, you need to use DATE_SUB. I'd recommend this query:
SELECT *
FROM records
WHERE today >= DATE_SUB(CURRENT_DATE, INTERVAL 3 DAY)

Filter an html table bound to a MySQL table by date

I have an html table that displays maintenance records. There are 3 columns that have dates in the future.
I want to have a button above the table that checks whether any of these 3 dates are within the next 30 days. If so, the row is displayed and other rows that are not of immediate concern are not displayed.
What is the best approach for achieving a filter like this?
Update: I'm trying to do it in a MySQL query.
I have 3 attributes in the SQL table that are date.
Does anyone know how to query whether the dates are within the next 30 days?
Does anyone know how to query whether the dates are within the next 30 days?
Use this :
WHERE yourdate BETWEEN CURDATE() AND DATE_ADD(CURDATE(),INTERVAL 30 DAY)
uses between, curdate() and date_add()
Update
to check multiple dates you need to do :
WHERE yourdate BETWEEN CURDATE() AND DATE_ADD(CURDATE(),INTERVAL 30 DAY)
AND yournextdate BETWEEN CURDATE() AND DATE_ADD(CURDATE(),INTERVAL 30 DAY)
AND anotherdate BETWEEN CURDATE() AND DATE_ADD(CURDATE(),INTERVAL 30 DAY)

MySQL select rows from exactly 7 days ago

I'm completely stumped on this one, being trying for hours but with no success, hoping someone can help. Trying to build a cron script to run daily that returns the rows that are exactly 7 days older than the current date.
The thing is, my query is not returning anything. No error messges, nothing (I know there are entries in the DB from the last 7 days - we get about 7000 new entries a day, so they are there!) I've tried a SELECT * and echo out the edit date with success, so everything is working, apart from my SQL script.
The column I'm referencing (edit_date) is type 'datetime' formated with Y-m-d h-m-s. This column always has a datetime value assigned on both create and edit.
function get_ad_sql($table){
$sql = "SELECT
*
FROM
".$table."
WHERE
edit_date = DATE_SUB(NOW(), INTERVAL 7 DAY)
";
return $sql;
}
And calling the function and 'trying' to echo the primary_key:
$sqlAng = get_ad_sql('angebote');
$result = mysql_query($sqlAng);
while($row = mysql_fetch_array($result)){
echo $row['primary_key'];
}
I've tried every variation of DATE_SUB(NOW(), INTERVAL 7 DAY), including CURDATE(), DATE_FORMAT(edit_date, '%m/%d/%Y') that I could find on here and online, but couldn't get anything to work. Hope someone can help me!
It is very rare to get same datetime entries which gives date and time upto seconds. Therefore, for getting appropriate results we need to ignore the time part, and deal with date part, thus, using CURDATE() function.
You could do that ignoring the time part and compare with the date using following:
function get_ad_sql($table){
$sql = "SELECT
*
FROM
".$table."
WHERE
DATE(edit_date) = DATE_SUB(CURDATE(), INTERVAL 7 DAY)
";
return $sql;
}
NOW() returns DATETIME value, you should use a DATE function to get date without time, e.g. -
SELECT * FROM table WHERE edit_date = DATE_SUB(DATE(NOW()), INTERVAL 7 DAY);
If type of edit_date field is DATETIME, then this field should be wrapped by DATE() function too -
SELECT * FROM table WHERE DATE(edit_date) = DATE_SUB(DATE(NOW()), INTERVAL 7 DAY);
Your script is working... I highly doubt you have something exactly 7 days ago (to the second).
Perhaps you wanted something WHERE edit_date>DATE_SUB(NOW, INTERVAL 7 DAY) AND edit_date<DATE_SUB(NOW, INTERVAL 6 DAY)?
Or, if you want to compare just the date (not the time) portions, compare the output of DATE() instead.
SELECT SUBDATE(CURDATE(), 7)
Try this.

Categories