MySQL - Select Dates that are 1 week intervals from today - mysql

I have a MySQL db that stores orders, and has a date field that gets populated when the order reaches a certain point.
I want to create a cron job that checks for all orders where this date is in multiples of 'weeks' ago. For example:
Date stored: 12/1/2012
this row would be returned if the cron job triggered on the following days:
12/8/2012
12/15/2012
12/22/2012
12/29/2012
etc...
How do i structure the MySQL query to fetch data in this way?

You can use modular arithmetic:
SELECT * FROM my_table WHERE DATEDIFF(CURDATE(), my_date) % 7 = 0

Related

MySQL - group by interval query optimisation

Some background first. We have a MySQL database with a "live currency" table. We use an API to pull the latest currency values for different currencies, every 5 seconds. The table currently has over 8 million rows.
Structure of the table is as follows:
id (INT 11 PK)
currency (VARCHAR 8)
value (DECIMAL
timestamp (TIMESTAMP)
Now we are trying to use this table to plot the data on a graph. We are going to have various different graphs, e.g: Live, Hourly, Daily, Weekly, Monthly.
I'm having a bit of trouble with the query. Using the Weekly graph as an example, I want to output data from the last 7 days, in 15 minute intervals. So here is how I have attempted it:
SELECT *
FROM currency_data
WHERE ((currency = 'GBP')) AND (timestamp > '2017-09-20 12:29:09')
GROUP BY UNIX_TIMESTAMP(timestamp) DIV (15 * 60)
ORDER BY id DESC
This outputs the data I want, but the query is extremely slow. I have a feeling the GROUP BY clause is the cause.
Also BTW I have switched off the sql mode 'ONLY_FULL_GROUP_BY' as it was forcing me to group by id as well, which was returning incorrect results.
Does anyone know of a better way of doing this query which will reduce the time taken to run the query?
You may want to create summary tables for each of the graphs you want to do.
If your data really is coming every 5 seconds, you can attempt something like:
SELECT *
FROM currency_data cd
WHERE currency = 'GBP' AND
timestamp > '2017-09-20 12:29:09' AND
UNIX_TIMESTAMP(timestamp) MOD (15 * 60) BETWEEN 0 AND 4
ORDER BY id DESC;
For both this query and your original query, you want an index on currency_data(currency, timestamp, id).

MySQL DATE_ADD?

01) I have a MySQL table named issue. It is storing book details as book_no, book_name, is_date etc.. issued for various members.
02) I need to select list of books that should return on current date. The return date is calculated by adding 3 days to the is_date. I used following script. But I returned an empty result.
Variables used to Store return dates
$rdate=mysql_fetch_array(mysql_query("SELECT * FROM issue"));
$is_date=$rdate['is_date'];
$rt_date=mysql_fetch_array(mysql_query("SELECT
DATE_ADD('$is_date', INTERVAL 3 DAY) FROM dual"));
SELECT statement
SELECT issue.b_no, issue.b_name, issue.is_date
FROM issue WHERE '$rt_date' = CURDATE()
03) I can not understand what I am doing wrong.

mysql filter by date and time separately

I have to perform a query on a MySQL database.
I have a table with records, have a column called "date" (the date type), and a column called "time" (type. Integer is stored by multiplying the time of day by 60. eg 8 am is stored as 480).
Unfortunately, the format of this table can not be modified.
My table stores attentions of doctors on call. The doctors on duty working in two shifts: from 8-20, and 20-8.
I need to know the amount of attention for every doctor.
My query must be filtered by date range and shift.
The problem is that, in the case of doctors working at the turn of 20-8, I have to consider a change of day. (sorry for my bad English).
What I have done is this, this would be an example to date of yesterday, and doctors shift 20-8.
SELECT * FROM attentions WHERE (date >= '2015-07-23' and time >=1200) and (date <= '2015-07-24' and time <480)
the query does not work at all.
Supposing the date field is called: 'a_date' with format 'yyyy-mm-ss' and the time field is a number, the query should be:
SELECT * FROM attentions WHERE (date(a_date) >= '2015-07-23' and time >=1200) and (date(a_date) <= '2015-07-24' and time <480)
Can you check using between?
SELECT * FROM attentions WHERE date between '2015-07-23' and '2015-07-24' and time between 1200 and 480
I think you can also use this -
SELECT * FROM ***** where CREATED_DATETIME between '2015-03-12 00:00:00' and '2015-05-11 00:00:00';

fetch last 5 minutes of data from oracle table

Hi i am trying to fetch last 5 minutes of data from oracle table.The query is written below and its not working somehow.
select * from mytable where (time_to_sec(timediff(now(),mytable.time_stamp)) <= 300)
Its showing this error ORA-00904.
I tried one more query.
select * from mytable where TIME_STAMP > (sysdate - numtodsinterval(5,'minute'))
Now, can you tell me the query which fetches data of last 5 minutes and which deletes data that is in the table for more than 12 hours.Thanks.
I need queries in both oracle and mysql. The mysql query i tried is here.
delete from mytable where (time_to_sec(timediff(now(),time_stamp))/3600 >12);
In oracle subtracting 1 from timestamp means one day. And You can substract a fraction of one. So,
current_timestamp - (5/(24*60))
gives You date from 5 minutes ago. Using that we can query:
select * from mytable where TIME_STAMP > current_timestamp - (5/(24*60)
Which should give You needed result. I find this method more straightfoward and simpler to remember than using special functions.
If You want filter out data from last 12 hours than You can query it like this:
select * from mytable where TIME_STAMP <= current_timestamp - 0.5

Single DB query for fetching the business days (mysql)

I would like to know if it's possible to fetch X no of business days (date datatype) via a single DB call in mysql.
The list of holidays are stored in a table. So, the days (starting from CURDATE) which do not have entries in that table are considered to be working days.
Thanks!
Does the holiday table also include weekends?
Create a numbers table with a single column (num, say) and rows 1 through some-large-value - this'll come in handy. LEFT JOIN the holidays table to this table on "holidayday" = (CURDATE + INTERVAL num - 1 DAY), add a WHERE to exclude the holidays and then order this query by num ascending and LIMIT the query to the X rows.