MYSQL get last date from table that has not yet passed [closed] - mysql

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
In my database in table_a every row has a date_created like "2011-04-17"
Now some of these dates are in the past, but my question is how can I retrieve the latest date that has not yet passed?

Try this one
SELECT * FROM table_a WHERE CURDATE() <= date_created
CURDATE()
Returns the current date as a value in 'YYYY-MM-DD' or YYYYMMDD
format, depending on whether the function is used in a string or
numeric context.

Can you try this,
SELECT * FROM table_a WHERE date_created >= CURDATE()

IF date_created is a date datatype then you can use
SELECT *
FROM table_a
WHERE date_created >= NOW()
LIMIT 1

Related

How to write query to retrieve latest time value closest to today in SQL? [closed]

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 1 year ago.
Improve this question
How to retrieve latest value for each card from time from the table in SQL including sensor id and event type closest to todays date please?
create table events (
sensor_id integer not null,
event_type integer not null,
value integer not null,
time timestamp unique not null
);
I have tried code below
SELECT TOP 1 *
FROM events
WHERE events.time < "2021-01-01 00:00:01"
ORDER BY events.time DESC
Top 1 does not work in mysql and limit 1 did not get me the results.
I need to get latest time value for each sensor_id not only the latest value for the whole table.
You can use order by and limit. Let me assume that you mean on or before today:
select e.*
from events e
where e.timestamp <= now()
order by e.timestamp desc
limit 1;
If you want future dates as well, this can be tweaked.

MS SQL query converted to MySQL [closed]

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

mix 2 mysql date columns to one and order by [closed]

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 have a table "USER" in MySQL , some columns are "ID","Name","Birth" and "Death"... Birth and Death save date values.
I want to display both Birth and Death and then order by date ASC.
Here is an example from what I have and what I need..
column Birth:
1-2-99
2-2-99
column Death:
1-1-99
2-3-99
desired result from a query:
1-1-99 (from death col)
1-2-99 (from Birth col)
2-2-99 (from Birth col)
2-3-99 (from death col)
The example is displayed as (DD,MM,YY)
Use UNION or UNION ALL:
SELECT `date`
FROM
(
SELECT death AS `date` FROM tablename
UNION ALL
SELECT birth FROM tablename
) AS t
ORDER BY `date`;

Need to create a query for monthly data [closed]

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";

return no or yes if time difference is greater than 5min [closed]

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;