I am trying to find out the date and time of last 5 days from the database,the data inserted into the table is in the form of date("Y-m-d h:i:s");
Now when i try retrieving it the values do not show up until the time from the table is removed
My Query is :
SELECT Key,DATE_FORMAT(lastUpdated, '%D %M %Y')
FROM table
WHERE lastUpdated Between NOW() - INTERVAL 5 DAY AND NOW()
ORDER BY Key DESC;
I believe you want to just compare the DATE() of the lastUpdated
SELECT Key,DATE_FORMAT(lastUpdated, '%D %M %Y')
FROM table
WHERE DATE(lastUpdated) Between NOW() - INTERVAL 5 DAY AND NOW()
ORDER BY Key DESC;
Related
I need help with some dates in mysql
I have this query that brings me the count of the messages of the corresponding dates of every message from the last 30 days.
SELECT DATE_FORMAT(time, '%m/%d/%Y') AS Dates, count(*) as count
FROM ma_messages
WHERE time BETWEEN NOW() - INTERVAL 30 DAY AND NOW()
AND usersid_send = 110
Group by Dates
ORDER BY Dates ASC
query image
But I need that also bring me the other dates of the last 30 days with a value in the count of 0, for example that in the query also bring me 05/26/2021 date and the dates before 05/25/2021 and after 05/28/2021.
I don't know if this is possible, but I will apreciate any help.
Thanks.
try this
SELECT
DATE_FORMAT(time, '%m/%d/%Y') AS Dates,
count(if(usersid_send='110',true,null)) as count
FROM ma_messages
WHERE time BETWEEN NOW() - INTERVAL 30 DAY AND NOW()
Group by Dates
ORDER BY Dates ASC
I have a table with dates .. with all the dates of the year – Alexis Murillo
SELECT d.`date`, COUNT(m.`time`) AS `count`
FROM dates_table d
LEFT JOIN ma_messages m ON d.`date` = DATE(m.`time`)
WHERE d.`date` BETWEEN NOW() - INTERVAL 30 DAY AND NOW()
AND m.usersid_send = 110
GROUP BY d.`date`
ORDER BY d.`date` ASC
Using your table "with all the dates of the year":
WITH
message_counts AS ( SELECT DATE_FORMAT("time", '%m/%d/%Y') AS "day",
COUNT(*) AS "count"
FROM ma_messages
WHERE "time" BETWEEN NOW() - INTERVAL 30 DAY AND NOW()
AND
usersid_send = 110
GROUP BY Dates)
SELECT all_dates."day", COALESCE(message_counts."count", 0)
FROM all_dates
LEFT JOIN message_counts
ON all_dates."day" = message_counts."day"
WHERE all_dates."day" BETWEEN BETWEEN NOW() - INTERVAL 30 DAY AND NOW()
ORDER BY all_dates."day" ASC
I'm trying to make a chart where the X axis is the date (last 14 days) and the chart itself shows the count of the post on that date. However, it starts with 1. oct -> 9. oct and then proceeds with 25. sep -> 30.sep
My SQL:
SELECT DATE_FORMAT(created_at, "%d. %b") AS date, count(*) as count
FROM posts
WHERE created_at BETWEEN NOW() - INTERVAL 14 DAY AND NOW()
Group by date
ORDER BY date ASC
Image of the issue: https://i.imgur.com/d8AIRu6.png
The problem is that date in the order by clause references the alias defined in the select clause, that is a string representation of the date. You can't use that to sort the resultset as you want.
Here is one workaround: add another expression to the group by clause that has a date datatype - you can then use it to order the results:
SELECT date_format(created_at, '%d. %b') AS date, count(*) as count
FROM posts
WHERE created_at BETWEEN NOW() - INTERVAL 14 DAY AND NOW()
Group by date, date(created_at)
ORDER BY date(created_at) ASC
i am try to request date only yesterday but without success...
My query request.
SELECT registeredDaySell FROM new_sell WHERE DATE_SUB(CURDATE(), INTERVAL 1 DAY)
My date is organized this way.
16 September, 2017
Thanks for helping me out.
subdate(now(),1) will return yesterdays timestamp
The below code will select all rows with yesterday's timestamp from employee_login page
Select * FROM `employee_login` WHERE `dattime` <= subdate(now(),1) AND `dattime` > subdate(now(),2)
The below code will display yesterday's timestamp
Select subdate(now(),1) ,subdate(now(),2))
This will give
SELECT producFinalPrice
FROM new_sell
WHERE WEEK (date) = WEEK( current_date ) - 1
As #Gordon mentioned, you should consider storing your dates either in some date type column, or possibly as a UNIX timestamp (seconds since the epoch). A possible workaround here would be to use STR_TO_DATE to convert your string dates to bona fide dates on the fly.
SELECT
producFinalPrice
FROM new_sell
WHERE
STR_TO_DATE(date_col, '%d %M, %Y') = DATE_SUB(CURDATE(), INTERVAL 1 DAY)
This assumes that date_col is the name of the column in your table which contains string dates.
SELECT producFinalPrice FROM new_sell
WHERE where date >= DATEADD(day, -1, convert(date, GETDATE()))
and date < convert(date, GETDATE())
-1 equates to "today" minus 1 day. You can change that number to get the number of days that you want to go back if further than 1.
I have made this query and it works correct to show data for a month from the current date. For example this query:
SELECT *
FROM test
WHERE price >=100
AND active = 1
AND dateadded >= UNIX_TIMESTAMP(DATE_SUB( CURDATE(), INTERVAL 1 MONTH))
ORDER BY testvalue DESC
This query outputs date time stamp from i.e.30th October till the current date 30th November. I have edited the query and takeout CURDATE but it fails at the point.
The question is how would I change this query to just show only last months i.e. October's data if the month is November.
You have to get the range between the first day of the previous month, which you can get by formatting the date and setting the day part to 01 and the last day of the previous month. There's a function for that. On the same page in the manual you also find information about DATE_FORMAT().
SELECT *
FROM test
WHERE price >=100
AND active = 1
AND dateadded BETWEEN
UNIX_TIMESTAMP(DATE_FORMAT(DATE_SUB( CURDATE(), INTERVAL 1 MONTH), '%Y-%m-01'))
AND UNIX_TIMESTAMP(CONCAT(LAST_DAY(DATE_SUB( CURDATE(), INTERVAL 1 MONTH), ' 23:59:59'))
ORDER BY testvalue DESC
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())