MySQL Date and Interval - records between dates - php

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)

Related

Why is this not selecting only records with a date older than 7 days?

I am trying to filter for rows that have a value of '1' in the active column and where the date in the lastfollowupemail_date column is 7 days or older from the current date.
Searching on the internet on many sites, this is the query I came up with, but for some reason it is not filtering correctly (the query is selecting records with a date less than 7 days from the current time).
Any ideas on why this may be occurring would be fantastic and much appreciated. Thank you so much in advance!
SELECT * FROM e_sales_prospects
WHERE active='1'
AND lastfollowupemail_date > DATE_SUB(NOW(), INTERVAL 7 DAY)
if I'm reading this right you want rows that are older then seven days, in that case you want rows which have date smaller then so in my opinion it should be
lastfollowupemail_date < DATE_SUB(NOW(), INTERVAL 7 DAY)

Correct way to fetch posts which are posted 3 days ago?

My data has a column named "date" which includes post dates in unix date format. I am using this query to fetch posts which are posted 3 days ago
$result=mysql_query("SELECT * FROM track WHERE
FROM_UNIXTIME(date,'%Y-%m-%d %h:%m:%s') > NOW() - INTERVAL 3 DAY AND
FROM_UNIXTIME(date,'%Y-%m-%d %h:%m:%s') < NOW() - INTERVAL 2 DAY");
This works, however, it selects posts by date which are in range of -48 hours and -72 hours.
But I want to fetch posts by day names. I mean, for example if a post added 40 hours ago, now its not theorically 48 hours (2 days) but if we look at the day name, it was 2 days ago. How can I fetch posts like that ?
SELECT * FROM track WHERE
FROM_UNIXTIME(date,'%Y-%m-%d') = CURDATE() - INTERVAL 2 DAY

Getting mysql result from the last 30 days [duplicate]

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'

php mysql search birthday between two dates

I need to find the birth day of people from the table .. coming in next 7 days from today.
I have a query ..SELECT * FROMtableWHEREdobLIKE BETWEEN %-08-17 AND %-08-24 but it returns the records whose dates are not submitted in database..i mean the entry is 0000-00-00
I have stored the birthdates in dates format in table. Please Help me finding the bug.
Since this is mysql, I don't know if DATE_FORMAT() can work on this. But give this a try.
SELECT * FROM users WHERE DATE_FORMAT(dob, '%c-%d')
BETWEEN DATE_FORMAT('1983-08-17', '%c-%d')
AND DATE_FORMAT('1983-08-24', '%c-%d') OR (MONTH('1983-08-17') > MONTH('1983-08-24')
AND (MONTH(dob) >= MONTH('1983-08-17')
OR MONTH(dob) <= MONTH('1983-08-24')))
any year can be used (just to complete the date format) since year does not matter
UPDATE 1
Tested it on SQLFiddle.com
SQLFiddle Demo
UPDATE 2
I'm sorry for my first answer. I honestly missed to read this line coming in next 7 days from today. And I think that was the reason why I was downvoted by Imre L. He has his point. The reason why I posted the answer like that was because I thought the OP was asking for the days in between regardless of the year. So here is the update.
SELECT ....
FROM ....
WHERE DATE(dob) BETWEEN NOW() AND NOW() + INTERVAL 7 DAY
Hope it's clear now. :D
this will handle correctly cases wen there is a month or year change between the date range:
select *
from people
where (DAYOFYEAR(dob)+IF(DAYOFYEAR(CURDATE())>DAYOFYEAR(dob),1000,0))
between DAYOFYEAR(CURDATE())
and (DAYOFYEAR(CURDATE() + INTERVAL 7 DAY)+IF(DAYOFYEAR(CURDATE())>DAYOFYEAR(CURDATE() + INTERVAL 7 DAY),1000,0))
By converting the dob date into this year's date you can avoid issues where the period crosses a month or year boundary. This selects all rows where the birthdate occurs in the coming week:
SELECT * FROM users
WHERE concat( year(now()), mid(dob,5,6) )
BETWEEN now() AND date_add(now(), interval 7 day)
SELECT
str_to_date(DATE_ADD(dob, INTERVAL (YEAR(CURRENT_DATE()) - YEAR(dob)) YEAR), '%Y-%m-%d') BIRTHDAY,A.*
FROM app_membership A
WHERE str_to_date(DATE_ADD(dob, INTERVAL (YEAR(CURRENT_DATE()) - YEAR(dob)) YEAR), '%Y-%m-%d')
BETWEEN str_to_date('15-10-2017','%d-%m-%Y') and str_to_date('10-11-2017','%d-%m-%Y')
ORDER BY BIRTHDAY ASC;
Try this. Worked for me.
Lets list people who born in any month/year between december 14 and august 24. We know its an year before another one. We must count with months in the previous year. It's complex because you may have a problem comparing the month of the starting date with the month of the ending date. However, it may be solved with this query:
SELECT * FROM t_users WHERE (DATE_FORMAT(d_birth, '%m-%d')
BETWEEN DATE_FORMAT('2017-12-14', '%m-%d') AND
DATE_FORMAT('2018-08-24', '%m-%d'))
OR(MONTH('2017-12-31') >= MONTH('2018-08-24')
AND (MONTH(d_birth) >= MONTH('2017-12-31')
OR MONTH(d_birth) <= MONTH('2018-08-24')))

MySQL: Getting results present in one time frame but not in another time frame

I have generated a dataset that contains data spanning thirty days. Im trying to issolate new data elements that have appeared in the last 2 days but not in the previous 28 days before that.
I run a PHP script that generates the test data. (PHP and MYSQL return the same time when tested)
I run the following query against it.
Results are returned accuretly for aproximetly half an hour. Then despite the fact I believe there to be matching records none are returned when running this query.
Is there any obvious mistake I'm making in the SQL that would cause this apparent 'drift' to occur?
About The Data:
The script generates a 'race' per day. It populates the ranking tables with ranking of the 10 'jokeys'. For the purposes of testing the script generates races from the previous 2 days with 2 new 'jokeys' in the top 10. The remaining 30 days the races are identical.
Results Expected:
The names of two jokeys who have recently ranked in a race (in the last two days and have not ranked in the previous 28).
The SQL:
SELECT *, FROM_UNIXTIME(`race_timestamp`) as ts FROM `rankings`
WHERE `race_venue` = UNHEX(MD5('someplace'))
AND `jokey` IN
(
SELECT `jokey`
FROM `rankings`
WHERE `race_timestamp`
BETWEEN # Get results for races between now and two days ago
UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 2 DAY)) # timestamp two days ago
AND
UNIX_TIMESTAMP() # time stamp now
)
AND
`jokey` NOT IN
(SELECT `jokey`
FROM `rankings`
WHERE `race_timestamp`
BETWEEN # Get results between 2 and 30 days ago
UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 DAY)) # time stamp 30 days ago
AND
UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 2 DAY)) # time stamp 2 days ago
)
GROUP BY jockey;
Hope someone can help! Ben
If you want to do this by date, rather than by the exact minute and second, you might change:
UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 DAY))
to something like:
DATE(DATE_SUB(NOW(), INTERVAL 30 DAY))
Well there are some small errors in your SQL. You switch between jokey and jockey I'm going to just guess you mean jockey since this seems race-related. Not sure if this is in your actual code but it probably wouldn't run at all if it was. Also, you have no reason to use GROUP BY jockey as there are no aggregate functions being used.
Try this:
SELECT *, FROM_UNIXTIME(race_timestamp) AS ts FROM rankings
WHERE
race_venue = UNHEX(MD5('someplace'))
AND jockey IN (
SELECT jockey FROM rankings
WHERE race_timestamp
BETWEEN UNIX_TIMESTAMP(DATE_SUB(DATE(NOW()), INTERVAL 2 DAY))
AND UNIX_TIMESTAMP()
)
AND jockey NOT IN (
SELECT jockey FROM rankings
WHERE race_timestamp
BETWEEN UNIX_TIMESTAMP(DATE_SUB(DATE(NOW()), INTERVAL 30 DAY))
AND UNIX_TIMESTAMP(DATE_SUB(DATE(NOW()), INTERVAL 2 DAY))
)
Other than that, there are no actual errors I can detect in your logic. This query should return all jockeys for the requested venue who are ranked within the past 2 days and weren't ranked within 2-30 days ago.

Categories