Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have two DateTime columns like start_datetime and end_datetime. these fields contain DateTime value.
Here itself I need three different outputs. while you search, "15-09-2020 00:00:00" to "26-09-2020 23:59:59" it will display the first two lines. if you search like "26-09-2020 00:00:00" to "13-10-2020 23:59:59", it should result in three rows.
Instead of if you search, "10-09-2020 00:00:00" to "18-09-2020 23:59:59", it will show the first two rows.
In this case, I am stuck at the query. if I am using between some results won't come. if I am using greater than a symbol, some results won't come.
store the date time format in YYYY-MM-DD H:I:S, so you can easily
SELECT *
FROM table_name
WHERE start_datetime BETWEEN value1 AND value2 AND end_datetime BETWEEN value1 AND value2;
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 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.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
i want to select info which was registered in database earlier first one by one,
its i want to select oldest info first then newer, like to be in order (first come first serve) one by one
like
Today - some info
Yesterday - some info
day before yesterday - some info
SELECT info FROM table ORDER BY A DESC LIMIT 1
table
id info date_registered
1 john 9999-12-31 23:59:59
Your question is vague, but there's various ways you can do it.
What are you doing with this?
check out OFFSET and LIMIT, which you'd do something like run your query and then increase the offset each time you run it.
just use whatever programming language you're using to loop over the result set? IE remove the limit.
3 it multiple times and add a where clause which uses the previous return value.
SELECT info
FROM table
where date_registered < '9999-12-31'
order by date_registered desc
limit 1
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 4 years ago.
Improve this question
Table structure
id from(date)_field to(date)_field field3 field4
1 date1 date2 something something
2 date3 date4 something something
3 date5 date6 something something
. . . . .
. . . . .
I want to find out the top 1 row where the from(date)_field is between the user_input_date and from(date)_field.
You can do:
select t.*
from t
order by abs(datediff(fromdate, #yourdate))
limit 1;
More typically, you would not want the closest date. You would want the row where the date is between the to and from dates. However, you are explicitly asking for the closest.
Use datediff and find the smallest value.
SELECT DATEDIFF(second, fromdate, Userdate) AS DateDiff;
SELECT DATEDIFF(second, Todate, Userdate) AS DateDiff1;
And compare DateDiff and DateDiff1 which have lowest value that would be closest one
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
In my table, there is one filed named "Reporting_Date". And I want to get Weekday name of all dates from table. Weekday like sunday, monday. How does it possible in mysql?
Use DAYNAME(date)
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_dayname
mysql> SELECT DAYNAME('2007-02-03');
-> 'Saturday'
SELECT DAYNAME(Reporting_Date) FROM <table_name> WHERE 1
I guess you could model date format according to your requirements.