Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
I have a column name called last_activity that is of type DATETIME. (so. 2021-01-03 00:00:00). I want to find all rows which are within 5 minutes of now. I attempted the following:
SELECT *
FROM users
WHERE last_activity > DATE_SUB(NOW(),INTERVAL 5 MINUTE)
I get 0 rows, even though the activity is within 5 minutes (I know because the database says so). How can I fix this?
Use the function TIMESTAMPDIFF() to get the difference in minutes:
SELECT *
FROM users
WHERE ABS(TIMESTAMPDIFF(MINUTE, last_activity, NOW())) <= 5
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to run a query in sequelize or SQL where select current month data and only show just upcoming date data like some kind of events if that day pass then next event or date will be showing like this I have 3 dates in this month
1 2020-03-15
2 2020-03-22
3 2020-03-27
So before 15 I want to only no 1 date after 15march pass I want only no 2 date and goes on
If you want to show the next date in the future, then it would be something like:
select t.*
from t
where date > current_date
order by date asc
fetch first 1 row only;
The exact syntax might vary by database -- say now() or getdate() instead of current_date; or select top (1) or limit 1 instead of the fetch clause.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a Mysql database that contains a list of names and dates of volunteers going back five years. What statement would I use to generate a list of new names after a specific date (i.e. people that have not volunteered in the past).
Here is an example, I have a list of 100 different volunteers who have volunteered many times on different dates over 5 years to the present. Some may have volunteered once, others 5 times, etc. Dave is a new volunteer who volunteered on February 6, 2015. I wish to generate a list of new volunteers after 2014-11-29. This list would pick up Dave's name only in this instance.
Try this:
SELECT name
FROM your_table
WHERE column_date < DATE_SUB(NOW(), INTERVAL 5 YEAR);
Or:
SELECT name
FROM your_table
WHERE column_date < DATE_SUB(CURDATE(), INTERVAL 5 YEAR);
EDIT (after the comments):
Try this:
SELECT DISTINCT aa.name
FROM your_table AS aa
WHERE aa.id NOT IN (
SELECT id
FROM your_table
WHERE column_date < '2014-11-29'
);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to be able to find all employees whose review is due in the next 45 days. I have the MS SQL:
Select *
from Employee
where DATEDIFF(yy,HireDate,GetDate()+45) > DATEDIFF(yy,HireDate,GetDate())
How does this convert to MySQL?
try:
Select *
from Employee
where DATEDIFF(HireDate,DATE_ADD(CURDATE(), INTERVAL 45 DAY)) > DATEDIFF(HireDate,CURDATE())
I think you can just use CurDate instead of GetDate, so it'd just be:
Select * from Employee where DATEDIFF(HireDate,CurDate()) > DATEDIFF(HireDate,CurDate())
You should only need to check DATEDIFF once. Just get the number of days and compare.
SELECT *
FROM Employee
WHERE DATEDIFF(CURDATE(), HireDate) <= 45
Docs for MySQL's DATEDIFF (and other date/time functions): http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to create a query in mysql to retrieve data where CREATED_DATE range should be 20th of current month to 20th of previous month. Please suggest..thanks
You can get dynamic records using current month, by the following query
SELECT * FROM table_name WHERE CREATED_DATE BETWEEN
DATE_FORMAT(NOW(),'%Y-%m-20') - INTERVAL 1 MONTH
AND DATE_FORMAT( NOW(),'%Y-%m-20')
Check the live result at the fiddle http://sqlfiddle.com/#!2/4668c/7
SELECT
*
FROM
your_table
WHERE
`CREATED_DATE` BETWEEN "2013-10-20" AND "2013-11-20";
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to write following query but i find id defecult for me.
select id,comment,comment_date from comments;
Here I want to add a field, if current time is more that 5 minutes of comment_date
then it should show no else yes.
How to write query for above.
One uses the date_sub function for date delta, taking a time and an interval, as follows:
select id
, comment
, comment_date
, case
when comment_date < date_sub(now(), interval 5 minute)
then 'yes'
else 'no'
end as answer
from comments;
SELECT
id,
comment_date,
CASE WHEN TIMESTAMPDIFF(MINUTE, comment_date, NOW()) > 5 THEN 'No'
ELSE 'Yes'
END AS isnew
FROM comments;