I'm being given a datetime String where I need to have two separate queries, one that gets all of the records in that week, and one that gets all the records in that month.
so for the example the week query: today is 2021-08-25 06:00:00 how would I get the records in that column where the earliest would be
2021-08-23 00:00:00 and the latest would be at the end of the week 2021-08-29 23:59:59
Same for a day in the month
Is using greater + less than the correct way to approach this requirement?
If you don't care about performance, you could just use:
where year(start_timestamp) = year(#date) and
week(start_timestamp) = week(#date)
And similarly:
where year(start_timestamp) = year(#date) and
month(start_timestamp) = month(#date)
Note that MySQL cannot use an index on this. This also only uses start_timestamp. Your question doesn't explain at all how two timestamps are supposed to fit into the criteria. And the definition of week is whatever week() decides it is.
Related
I have a MySql DB that consists of a table that holds all my data. I need to retrieve only those rows that have a startup_date that is less than or equal to today at 5 am UTC time. I tried looking up but it's kind of confusing. Can someone provide me a good, clean way of doing this?
My table is as follows:
tbl_MyData
***********
id name city startup_date
1 test1 New York 2020-01-10 18:19:30
2 test2 Houston 2019-01-30 05:00:00
3 test3 Chicago 2020-02-09 05:00:00
From the above data, my query should return ONLY row id number 2, since that is the only row that satisfies my criteria of startup_date being less than or equal to today at 5 am. Also, I'm not sure if I need to have any kind of UTC functions, since I'm not sure how mysql stores it's date time data.
Use a simple WHERE clause
SELECT t.* FROM mytable WHERE t.startup_date <= DATE_ADD(CURDATE(), INTERVAL 5 HOUR)
The CURDATE() function returns the current date without time (hence today at midnight), and the DATE_ADD() function adds 5 hours to it.
What SELECT statement have you tried also what are the data types for the table? If startup-date is a date type in MySQL then it should just be
SELECT * FROM tbl_MyData WHERE startup_date<=(whatever the value needs to be)
I am trying to get data by week, month and year.
I store date YYYY-MM-DD HH:MM:SS.
What I am doing is below;
Fetch one week old data;
query + AND WEEK(date) = WEEK(CURDATE())
Fetch a month old data;
query + AND MONTH(date) = MONTH(CURDATE())
The thing is I couldnt be able to get the data correct. For instance when I want to get week old data, I am gettin a year old one too.
Is there any other query that I could use? I have tried DATE(NOW()) - INTERVAL 30 DAY. It works but very slow.
Thanks!
I believe that the problem is that the WEEK function returns the week of the year. So, Jan 1st 2017 might be week 1 (also might be week 53 of the previous year depending on the day of the week and how MySQL handles it). But then, Jan 1st of 2016 is also week 1 - just for a different year.
Trying changing it to:
query + AND WEEK(date) = WEEK(CURDATE()) AND YEAR(date) = YEAR(CURDATE())
Also, if you're storing this as a string then definitely change it to a DATETIME
WHERE ...
AND date >= CURDATE() - INTERVAL 7 DAY
AND date < CURDATE()
Gives you the 7 days ending with yesterday. Use other techniques to get a particular month or week.
This technique is also much faster for large tables with a suitable index. Hiding date inside a function, such as WEEK() prevents the use of an index.
I have a MySQL DB table with multiple date type fields. I need to do different SELECT queries on this table but I am not sure which way is the best to find records from the same month.
I know I can do the following:
SELECT *
FROM table
WHERE MONTH(somedate) = 5
AND YEAR(somedate) = 2015
But I keep reading that isn't efficient and that I should go with using actual dates, i.e.
SELECT *
FROM table
WHERE somedate BETWEEN '2015-05-01' AND '2015-05-31'
However, all I would have is the month and the year as variables coming in from PHP. How do I easily and quickly calculate the last day of the month if I go with second option?
Don't calculate the last day of the month. Calculate the first day of the next month instead.
Your query can be like this
WHERE t.mydatetimecol >= '2015-05-01'
AND t.mydatetimecol < '2015-05-01' + INTERVAL 1 MONTH
Note that we're doing a less than comparison, not a "less than or equal to"... this is very convenient for comparing TIMESTAMP and DATETIME columns, which can include a time portion.
Note that a BETWEEN comparison is a "less than or equal to". To get a comparison equivalent to the query above, we'd need to do
WHERE t.mydatetimecol
BETWEEN '2015-05-01' AND '2015-05-01' + INTERVAL 1 MONTH + INTERVAL -1 SECOND
(This assumes that the resolution of DATETIME and TIMESTAMP is down to a second. In other databases, such as SQL Server, the resolution is finer than a second, so there we'd have the potential of missing a row with value of '2015-05-31 23:59:59.997'. We don't have a problem like that with the less than the first day of the next month comparison... < '2015-06-01'
No need to do the month or date math yourself, let MySQL do it for you. If you muck with adding 1 to the month, you have to handle the rollover from December to January, and increment the year. MySQL has all that already builtin.
date('t', strtotime("$year-$month-01")) will give days in the month
I currently have a table in my database which lists a range of dates, going from May until October, in the following format: YYYY-MM-DD
What I would like to do is create a new column with the days of the week, matching the date. I thought of two options;
Update all rows, in column 'day', starting with monday, then the next tuesday, next wednesday, etc, etc. and when reached sunday, loop the sequence until end of table.
Read the date in column 'date' and update the column 'day' with the matching day of the week.
I think option 1 is the most easy, and very well possible, because there are no dates being skipped. But I have no idea how to do this and couldn't find anything similar searching the web.
Any help would be greatly appreciated! Thank you
As a general rule, I would say: don't do it. The day of the week is derived directly from the date; storing it separately in the database breaks one of the basic rules of normalisation (no derived data).
There are MySQL functions to find the day of the week - dayofweek() and weekday().
Try this:
update table
set day = DATENAME(dw, date)
My db is made of groups of entries (by user) with a row for each day of the week and also groups where there is only 1 row per week of the year. This week may start Sat, Sun or Mon.
The sql groups all these rows by user id and works fine for the entries where the user has a row for every day
The problem I have is selecting the users rows where there is only one entry per week
Basically if the rows date is 11th Feb 2012 then I need to be able to select that row if the start date criteria falls on that date or within that following week and all rows upto but not including the row where the date column is after the end date
I'm trying everything like dateadd in the sql but I just cannot get it to add these rows in.
Hope I've made myself clear.
Say I have two entries in the db
2013-02-02
2013-02-09
I have a start date of 2013-02-05 and an end date of 2012-02-13
I need to get those two row as:
the start date falls on or within the week of 2013-02-02
and I also need 2013-02-09 as the end date falls on or within the week of that date.
Hope that makes it a bit clearer.
Not sure exactly what your question is asking.
If your field is of mysql date or datetime type, and you wanted to find if there was an entry for a given week could you not use MySQL WEEK Function to find all entries for the given week, you may also need to include a restriction on YEAR too.
You could also include the following week, but you may encounter problems. The main problem being week 52+1 of 2012 wont give week 1 of 2013, but week 1 of 2012.