i would like to count the date from the database where the month is now.
my code goes like
$sql="SELECT count(date) FROM tbl_upcoming where
date=MONTH(NOW())";
if ($results=mysqli_query($conn,$sql)){
$datecount=mysqli_num_rows($results);
}
for example: '2017-3-20', '2017-3-4', '2017-1-3', it will return 2.
Try something like this:
SELECT date, count(1) FROM tbl_upcoming where
MONTH(date)=MONTH(NOW()) and YEAR(date) = YEAR(NOW()) GROUP BY date
You were almost there:
SELECT count(*) FROM tbl_upcoming WHERE MONTH(the_date)=MONTH(NOW())";
The approach of MONTH(NOW()) is good, this will return you the current month.
However you were comparing the current month with a whole date, which can't work. You also have to apply the MONTH() function to your date to compare months with months
You should check like this,
month can be from past year or next year also,
select count(*) as cnt from tbl_upcoming
where MONTH(date) = MONTH(CURRENT_DATE) and YEAR(date) = YEAR(CURRENT_DATE);
I hope this will help.
You want to know how many rows are in the current month?
SELECT COUNT(*)
FROM tbl_upcoming
WHERE `date` >= CURDATE() - INTERVAL DAY(CURDATE())-1 DAY;
Notes:
In most situations, say simply COUNT(*).
CURDATE() is midnight this morning, hence easier to deal with for your query.
CURDATE() - INTERVAL DAY(CURDATE())-1 DAY gives you the DATE of the first of this month.
The formulation avoids all hiccups with various datatypes: DATE, DATETIME, DATETIME(6), TIMESTAMP, etc.
The next note handles leapdays, end of year, also.
If you have dates reaching into future months, then
add
AND `date` < CURDATE() - INTERVAL DAY(CURDATE())-1 DAY
+ INTERVAL 1 MONTH;
Related
I'm trying to display all attacks which have happened today. This is the current SQL I have in my php page. However it doesn't display how many have happened today.
$TodayAttacks = $odb->query("SELECT COUNT(id) FROM `logs` WHERE `date` BETWEEN DATE_SUB(CURDATE(), INTERVAL '-1' DAY) AND UNIX_TIMESTAMP()")->fetchColumn(0);
If by "today" you mean "since midnight today", your query should look something like this:
SELECT COUNT(id) FROM logs
WHERE date >= CURDATE();
If there is a possibility of date values in the log greater than today's date (hey, weird stuff happens!) then you want this (I advise against using BETWEEN for date comparisons):
SELECT COUNT(id) FROM logs
WHERE date >= CURDATE()
AND date < DATE_ADD(CURDATE(), INTERVAL 1 DAY);
If you mean "within the last 24 hours", your query would look more like this:
SELECT COUNT(id) FROM logs
WHERE date >= DATE_SUB(NOW(), INTERVAL 1 DAY);
Hope this helps.
EDITED per comments below
If the date column is a Unix timestamp value (stored as INT(11)?) then the query should look something like the following:
SELECT COUNT(id) FROM logs
WHERE date >= UNIX_TIMESTAMP(CURDATE());
One could also do the following to get records from the last 24 hours:
SELECT COUNT(id) FROM logs
WHERE date >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY));
SQL Fiddle here.
I have a MySQL table with the following columns:
id, month, day, remind_days_before
It's for a simple recurring reminder function. The user can enter for example: "I have a very important thing on the 5. April every year, please remind be 15 days before that". There could be tens of thousands of entries in this table. Using this table I want to run a cron_job every morning, which is sending out this reminders in form of email messages. The problem is I don't know how to write this kind of SQL query... Is it even possible? I want to query only those rows where the "month" and "day" as DATE is between TODAY and TODAY+31 days (this is the maximum number for remind_days_before).
I'm trying with this right now, but the $end_month is giving me the same as the $today_month:
$today_month = date('m');
$end_month = date('m', strtotime('+31 days', $today_month));
What happens when the +31 days DATE is in the next YEAR or we have leap year?!
Can someone help me out here?
Thank you very much.
First of all, your fields must be a no-mysql function.
So replace month/day with planning_month/planning_day, because the month and day words, are the functions of MySQL, and you may have errors if they are not quoted
Here's a working query. I've tested and works great:
select * from MyTable
/*CHECK CURRENT YEAR*/
where date(concat(year(now()), planning_month, planning_day)) =
date(date_add(now(), interval remind_days_before DAY))
OR
/*CHECK NEXT YEAR*/
date(concat(year(date_add(now(), INTERVAL 1 YEAR)), planning_month, planning_day)) =
date(date_add(now(), interval remind_days_before DAY))
Live Example
Your, it is a very useful question for me too
Just check for both years:
select *
from your_table
cross join (select year(now()) as current_year, year(now())+1 as next_year) vars
where curdate() + interval remind_days_before day = concat(current_year,'-',month,'-',day)
or curdate() + interval remind_days_before day = concat( next_year,'-',month,'-',day)
Hi i currently have two tables, one has daily entries and other gets the total of those and stores monthly values.
I'm able to get totals of each month and year with below query, but what i'd like to do is only get the months where month is greater than and equal to current month. btw date column is in date format (yyyy-mm-dd)
$revbyMonth = $conn->prepare("SELECT EXTRACT(MONTH FROM date) as month, EXTRACT(YEAR FROM date) as year, SUM(rev) as total FROM {$tempName} GROUP BY year, month");
You want to add a where clause immediately after the from clause. I think this will work for you. If the date has no time component:
where date > date_sub(curdate(), interval day(curdate()) day)
The expression date_sub(curdate(), interval day(curdate()) day) gets the last day of the previous month. If the dates have a time component:
where date >= date_sub(curdate(), interval day(curdate()) - 1 day)
should work.
Note: this is better than other methods that process date using functions, such as:
where year(date) > year(curdate()) or
(year(date) = year(curdate()) and month(date) >= month(curdate()) )
The use of the functions prevents MySQL from using an index on the date column.
I have date and time column in my table. I want to select all objects which has date and time larger than current time plus one hour:
I have tried the following:
SELECT * FROM mytable WHERE date >= CURDATE() AND time >= TIME(NOW()+INTERVAL 1 hour)
However this is logically not correct. If date is tomorrow and time is less than current HH:MM it will not select that record.
How can I use my date and time to compare with current datetime?
I am using php 5.2 with mysql
First of all, I recommend using a DATETIME column to make this filtering more efficent. However, this works if you cannot change:
SELECT * FROM mytable
WHERE (date = CURDATE() AND time >= TIME(NOW() + INTERVAL 1 hour))
OR date > CURDATE()
You can split your condition into two conditions...
When the date is equal to today and more than an hour ahead of the current time:
date == CURDATE() AND time >= TIME(NOW()+INTERVAL 1 hour)
When the date is greater than today:
date > CURDATE()
...and chain them together using OR and parentheses:
SELECT * FROM mytable WHERE (date == CURDATE() AND time >= TIME(NOW()+INTERVAL 1 hour)) OR date > CURDATE()
First check whether the date is today and if time is greater then current time. The second condition is to check whether the date is greater than current date. Because if the date is greater then current date then automatically it's time is ahead.
SELECT * FROM mytable
WHERE (date = CURDATE() AND time >= TIME(NOW() + INTERVAL 1 hour))
OR date > CURDATE()
SELECT * FROM mytable WHERE date >= CURDATE() AND time >= TIME(HOUR(NOW()) + 1);
or maybe just
SELECT * FROM mytable WHERE date >= CURDATE() AND time >= HOUR(NOW()) + 1;
As indicated by #Pekka, all the answers posted so far will give the wrong answer between 11pm and midnight. I don't work with MySQL, but I can look stuff up. A better approach seems to be the timestamp() function.
This reference states:
With two arguments, it adds the time expression expr2 to the date
or datetime expression expr1 and returns the result as a datetime value.
Why not use timestamp() with two arguments?
You can do this.
SELECT
*
FROM
mytable
WHERE
CONCAT( date, " ", time ) > (NOW() + INTERVAL 1 HOUR );
Personally I suggest that you get rid of the separate fields for date and time and create a consolidated 'DATETIME' field. I also suggest adding an INDEX to that field.
That would make your queries better and faster. :)
I am trying to fetch rows only from specific date (like today, yesterday or 2 days ago) in mySQL. I have a column named "date" in my rows. (which includes dates like 1365053426).
$result=mysql_query("SELECT count(*) as total from track WHERE `date` >= CURRENT_DATE
AND `date` < CURRENT_DATE + INTERVAL 1 DAY");
I have tried this query, but it returns "0". What is the correct way to do that ?
how about using BETWEEN?
SELECT COUNT(*) as TotalCount
FROM Track
WHERE Date BETWEEN CURDATE() + INTERVAL -2 DAY AND CURDATE()
How about using datediff() function?
SELECT count(*) as total from track WHERE datediff(now(),date)=interval day
note: interval day could be declare from 0 -> up depends on what previous date you want to show