Count on curdate() in mysql - mysql

I have a user_entry table which contains a date field. data type is datetime.
data base is mysql.
I want a count of current date and current month and all data of current date.
How can I get this?
I tried below query but it's not working.
select * from table
where DATE(date) = CURDATE()

SELECT date FROM `test` WHERE date = CURDATE()
or
SELECT date FROM `test` WHERE date = DATE('2016-04-04')
it's work.
if you want the number of matches:
SELECT COUNT(date) from test WHERE date = CURDATE()
What is the data type of field 'date'?
To obtain the DAY/MONTH you can use the corresponding functions
SELECT MONTH(date), DAY(date) from test
Moreover, you can use groups to create a complete report
SELECT COUNT(date), date from test GROUP BY DAY(date), MONTH(date)

i used below query and it works for me.
SELECT *
FROM `user_entry`
WHERE DATE( DATE ) = DATE( NOW( ) )

Related

SQL MAX datetime - 1 day

I want to do a comparison between two dates. The highest date (currently via MAX datetime) is working, but I can't get the day after the highest date to compare the data with.
I'm using the following to get the data of the highest available date:
SELECT `datetime`, `standardSubscriptionDuration`,
SUM(`activeStandardPriceSubscriptions`) AS OneMonthActiveStandard
FROM `Subscription_totals`
WHERE `standardSubscriptionDuration` = '1 Month'
AND `datetime` = (SELECT MAX(`datetime`) AS Date FROM `Subscription_totals`)";
I already tried:
(SELECT MAX(`datetime`) -1 AS Date
But this won't give the result. How am I able to get the data of yesterday and eventually compare them?
I think that you want the following date arithmetics:
WHERE
`standardSubscriptionDuration` = '1 Month'
AND `datetime` = (
SELECT MAX(`datetime`) - interval 1 day AS Date FROM `Subscription_totals`
)

MYSQL: Transfer values from one table to another based on date/hour

i have the following MYSQL tables:
Part of the input table 'input':
and part of the Output Table 'output':
both date/time columns are pre given and cannot be changed. they are the basis of the MYSQL code I want to use to do the following:
I want to sum all values from 'total' column of the input table from the same date and same hour and put them into 'total' column of the output table where the 'time' column has the same date and same hour as in the input table.
Would this be possible with MYSQL only?
Or do I need PHP as well?
The input table has about 400,000 values.
I started with:
SELECT SUM(total) FROM input
WHERE date BETWEEN '2019-06-06 00:00:00' AND '2019-06-06 23:59:59'
But I dont know how to continue.
Any help would be appreciated. Thanks in advance :)
Get the sums in the input table and join to the output table in the UPDATE statement:
update output o
inner join (
select date_format(date, '%Y%m%d%H') hour, sum(total) total
from input
group by date_format(date, '%Y%m%d%H')
) i on i.hour = date_format(o.date, '%Y%m%d%H')
set o.total = i.total
UPDATE outputtable,
(SELECT Sum(total) SUM,
Year(date) year,
Month(date) month,
Day(date) day,
Hour(date) hour
FROM inputtable
GROUP BY Year(date),
Month(date),
Day(date),
Hour(date)) InputT SET total = InputT.sum WHERE Year(outputtable.date) = InputT.year
AND Month(outputtable.date) = InputT.month
AND Day(outputtable.date) = InputT.day
AND Hour(outputtable.date) = InputT.hour;

date_trunc PostgreSQL function equal for mySQL

Im trying to retrieve data to make statistics, im using mySQL and i cant get the following function to work - the postgreSQL is working.
I want to retrieve the request for the last month and count the amount of new requests for each day.
postgreSQL
SELECT count(*), date_trunc('day', created_at) as date FROM requests
WHERE(created_at > '2014-08-13 00:00:00') GROUP BY 2 ORDER BY 2 ASC;
*mySQL - my code *
SELECT count(EXTRACT(DAY FROM created_at)), EXTRACT(DAY FROM created_at) as date
FROM `requests`
WHERE EXTRACT(DAY FROM NOW() - INTERVAL 1 MONTH)
GROUP BY date
Final code
SELECT count( * ) , date( created_at ) AS date
FROM `requests`
WHERE DATE( created_at ) > DATE( DATE_SUB( NOW( ) , INTERVAL 1 MONTH ) )
GROUP BY date
The equivalent for your case is date():
select date(created_at), count(*)
from requests
. . .
This isn't a general replacement, but it works to remove the time portion of a date.
EDIT:
Perhaps the better solution for these two databases is:
select cast(created_at as date)
This is ANSI standard and works in both these databases (as well as SQL Server). I personally don't use this in general, lest I accidentally use it in Oracle, causing difficult to find errors. (dates in Oracle have a time component, alas.)

How to count number of date between two days MySQL

On my mysql table I keep date as datetime. (eg: 2013-11-09 11:15:58), I want to write mysql Query to get number of dates to given date. can someone help me to write this..
eg:
Given date: 2014-01-19 10:15:15
On table 'date_added' has: 2013-11-09 11:15:58
is it possible to get number of dates using mysql query, like COUNT(), SUM()
I think your using TIMESTAMP to use this method
SELECT TIMESTAMPDIFF(DAY,'2003-02-01','2003-05-01') from tablename;
if you want to count following this code:
SELECT COUNT(*) FROM a1 WHERE '2014-03-01';
To find number of records on a specific date of given timestamp, use date_format or date functions on the field to filter by date.
select
date( date_added ) dt,
count( date_added ) dt_cnt
from my_table
group by
date_format( date_added, '%Y-%m-%d' );
Alternatively, you can also use date function to extract date part from a datetime column.
select
date( date_added ) dt,
count( date_added ) dt_cnt
from my_table
group by
date( date_added );
And if you want to count based on date part of an input datetime value then, use
select
date( '2014-01-19 10:15:15' ) dt,
count( date_added ) dt_cnt
from my_table
where
date( date_added ) = date( '2014-01-19 10:15:15' );
Refer to:
DATE(expr)
Extract the date part of a date or datetime expression
DATE_FORMAT(date,format)
Format date as specified

how to filter a select statement in mysql for a particular time period without using now()

I have tried to filter records but with the use of now function as given below
select * from table where date>= DATE_SUB( NOW( ) ,INTERVAL 90 DAY )
What I need is a select statement that can filter its records for a week or month from the current date but without using NOW() function
if you are using java you could make use of the following code
String timeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
or you could use curdate() of mysql
Since I found it hard to understand the question I provide the following possibilities:
Try for all dates in a week from now:
SELECT * FROM table
WHERE date BETWEEN CURDATE() AND DATE_ADD(CURDATE() ,INTERVAL 1 WEEK)
and for all dates in a month from now:
SELECT * FROM table
WHERE date BETWEEN CURDATE() AND DATE_ADD(CURDATE() ,INTERVAL 1 MONTH)
If you are looking for all dates of the current month use
SELECT * FROM table
WHERE MONTH(date)=MONTH(CURDATE()) AND YEAR(date)=YEAR(CURDATE())
or for all dates of the current week use
SELECT * FROM table
WHERE WEEK(date)=WEEK(CURDATE()) AND YEAR(date)=YEAR(CURDATE())