I have a table which contains records with datetime column. Records will be inserted into that table for every 3/5/8(dynamic) seconds.
Need: I have to show only one record per day. Here I have one condition that is the records should be inserted between 10.00 to 11.00 AM.
So I like to use that condition in where clause and use max to get the record. But I could not.
Please advice.
Try something like below:
select * from tblName where HOUR(datetime_column) between 10 AND 11 group by DAY(datetime_column)
Related
I have around 200,000 records and each record has DATETIME field. I have been trying to select records by every n hours using the DATETIME field. For example if n = 1; 1 record is selected for every 1 hour. I haven't been able to find many examples online.
Table: Product
Fields: id, name, description, lastSoldOn
Well, you can convert the date/time to seconds and use arithmetic to select one value from each n-hour period:
select min(datetimecol)
from t
group by floor(to_seconds(datetimecol) / (3600 * $n));
If you need the complete record, you can use join or exists to match back to the original table.
I've a table with lots of entries consisting of dates and a number.
For instance:
07.02.2016 - 12
06.02.2016 - 48
05.02.2015 - 24
...and so on.
Now I need to sum all of the values older than 2 months. For instance the 3rd entry (05.02.2015) will be added to the second (06.02.2016) and the second one should get the value 72 and the 3rd one should be deleted.
I'd like to know if there is some way to do this in mysql only?
Instead of writing the code for you, I'd like to merely give you some hints:
Identify which rows are older than 2 month and sum them up.
select sum(number) from table where date > curdate() + interval 2 months
or sth. similar will do.
Select the max. date of the entries that are smaller or equal to "now+2months".
Update that row with the value from step 1.
Delete the rows from step 1.
See here for details on date functions in MySQL.
This can be done in 2 statements (one for steps 1-3, one for the deletion).
I have a table which contains date (Field Type: Date and Date Format: %Y-%m-%d) as a field. I need to select all the rows from the table for all the years whose date is not between Dec 3rd and Dec 24th.
The table contains month and day as a separate fields.
The result can be obtained by using the following query:
select * from mytable where date not in (select date from mytable where month=12 and day between 3 and 24);
But i m trying to get the result in a single query like the below one but it gave empty rows:
select * from mytable where date not between '%Y-12-03' and '%Y-12-24';
Can it be done in a single query like the above one?
SELECT *
FROM mytable
WHERE MONTH(`date`) <> 12
OR DAY(`date`) NOT BETWEEN 3 AND 24
;
This will give you every row that meets the requirements. I'm sure someone has a faster way of doing this, since this will ignore all indexes and will likely be slow on a large dataset, but it does work and return the data you require, so if no-one can suggest an improvement this will answer your question.
Query 1:
SELECT *
FROM user_d1
WHERE EXISTS (SELECT 1
FROM `user_d1`
WHERE birthdate BETWEEN '1989-08-04' AND '1991-08-04')
ORDER BY timestamp_lastonline DESC
LIMIT 20
Query 2:
SELECT *
FROM user_d1
WHERE birthdate BETWEEN '1989-08-04' AND '1991-08-04'
ORDER BY timestamp_lastonline DESC
LIMIT 20
And what I really don't understand: why does Query 2 return the wrong results? It returns a list ordered first by birthdate and then by timestamp_lastonline...
Query 1 : If at least one record between the dates exists then the entire talbe is retrieved.
Query 2 : Only records between the dates are retrieved.
Read here for how EXISTS works.
Your second query uses BETWEEN to return every entry BETWEEN the first entry with '1989-08-04' and the next entry with '1991-08-04' and then orders these by timestamp_lastonline DESC. Note that this is literally returning the entries between the two entries with those two values, not every entry that has a year between 1989 and 1991(unless you manually ordered these to be chronologically indexed!). I'm interested to see what you think your first query returns, as it'll get every entry in the table ordered by timestamp_lastonline if there's a row that the BETWEEN clause returns.
I have a scenario where I want to be able to SELECT rows from a MySQL table, but exclude rows where the current time-of-day is inside a time-range.
Example:
The "quiet" period for one row is 10pm - 8:30am.
My SQL SELECT statement should not return that row if the current server time is after 10pm or before 8:30am.
Example 2: The "quiet period" is NULL and ignored.
Example 3: A new row is created with a quiet period from 9:53am to 9:55am. If the current server time is in that 2-minute window, the row is not returned by the SELECT.
My question:
What data format would you use in the database, and how would you write the query?
I have thought about a few different approaches (defining start_time as one column and duration as another, defining both in seconds... or using Date stamps... or whatever). None of them seem ideal and require a lot of calculation.
Thanks!
I would store the start and end dates as MySQL native TIME fields.
You would need to consider ranges that span midnight as two separate ranges but you would be able to query the table like this, To find all current quiet periods
SELECT DISTINCT name FROM `quiet_periods`
WHERE start_time<=CURTIME() AND CURTIME()<=end_time
Or to find all non-active quiet periods
SELECT name FROM quiet_periods WHERE name NOT IN (
SELECT name FROM `quiet_periods`
WHERE start_time<=CURTIME() AND CURTIME()<=end_time
)
So with sample data
id -- name -- start_time -- end_time
1 -- late_night -- 00:00:00 -- 08:30:00
2 -- late_night -- 22:00:00 -- 23:59:59
3 -- null_period -- NULL -- NULL
4 -- nearly_10am -- 09:53:00 -- 09:55:00
At 11pm this would return
null_period
nearly_10am
from the second query.
Depending on performance and how many rows you had you might want to refactor the second query into a JOIN and probably add the relevant INDEXes too.