MySQL Event to add booking on stored day and time - mysql

I'm creating a booking system and one of the requirements is for a mass 'Regular' booking to be added automatically.
I believe an easy solution to this would be to store all members 'regular' days in a table, then have a scheduled event that retrieves all the records and inserts each row as a record in the bookings table.
Regulars Table
|id | memberid | time | day | group |
____________________________________________
| 1 | 42 | 08:00 | Monday| 2 |
| 2 | 42 | 08:00 | Friday| 1 |
| 3 | 25 | 11:00 | Friday| 1 |
____________________________________________
As the days are stored as VARCHAR textual, i need a way to generate the next day as y/m/d. In PHP this would be date('Y-m-d', strtotime('next tuesday'));
So i'd like to know what the correct way to achieve the following:
for each row returned
insert into bookings (memberid,time,date,group) values (row[memberid], row[time], the next row[day], row[group]

This query gives you the next week day:
select id,
memberid,
t.time,
date_add(curdate(),
interval 6 +
FIND_IN_SET(t.day,'Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday')
- weekday(curdate()) day) as nextWeekDay
,t.group
from T
SqlFiddle demo

Related

Select all records that were created on a given day N months ago

Given a table orders:
+-----+---------+-------------------------+
| id | price | created_at |
+-----+---------+-------------------------+
| 1 | 16.50 | 2017-02-28 12:52:00.824 |
| 2 | 22.00 | 2017-10-03 15:12:39.107 |
| 3 | 50.00 | 2017-12-03 12:54:42.658 |
| 4 | 12.00 | 2018-01-02 07:21:47.808 |
| . | . | . |
| . | . | . |
| . | . | . |
+-----+---------+-------------------------+
and current date:
+---------------------+
| NOW() |
+---------------------+
| 2018-01-03 10:33:14 |
+---------------------+
I'd like to select all records that were created on current day any months ago. So for above data my query should return:
+-----+---------+-------------------------+
| id | price | created_at |
+-----+---------+-------------------------+
| 2 | 22.00 | 2017-10-03 15:12:39.107 |
| 3 | 50.00 | 2017-12-03 12:54:42.658 |
+-----+---------+-------------------------+
But there are some edge cases for the last day of month:
if it's 31-days month, it's trivial
if it's 30-days month, the query should return all records created on 30th and 31st day of month
if it's February in a leap year, the query should return all records created on 29th, 30th and 31st day of month
if it's February in a normal year, the query should return all records created on 28th, 29th, 30th and 31st day of month
What I have tried is something like this:
SELECT * FROM orders
JOIN (
SELECT id, PERIOD_DIFF(
DATE_FORMAT(NOW(), "%Y%m"),
DATE_FORMAT(created_at, "%Y%m")
) AS diff
FROM orders
) AS periods
ON orders.id = periods.id
WHERE DATEDIFF(created_at + INTERVAL diff MONTH, NOW()) = 0;
But it doesn't cover the edge cases and I believe there is a smarter way (maybe without a subquery) to achieve the expected results.
EDIT:
To give you more context - what I need is a kind of a loop. I have a cron job scheduled to run once a day at midnight. This job should select all ids of orders that were created on this day any months ago and then refresh some other data associated with those ids. The important part is to refresh this data exactly once every month - that's why the last day of months is so crucial.
For example, given following creation dates:
DATES = [
2015-05-30, 2016-02-29, 2016-10-03,
2016-12-31, 2017-05-28, 2018-01-03
]
+---------------+------------------------------------+
| NOW() | SHOULD BE INCLUDED |
+---------------+------------------------------------+
| 2018-01-03 | 2016-10-03, 2018-01-03 |
| 2018-02-28 | 2016-02-29, 2016-12-31, 2017-05-28 |
| 2018-04-30 | 2015-05-30, 2016-12-31 |
| 2018-10-31 | 2016-12-31 |
+---------------+------------------------------------+
Can I suggest a slight simplification Walerian?
SELECT
*
FROM
orders
WHERE
(
DAYOFMONTH(created_at) = DAYOFMONTH( NOW() ) --monthdays that match
)
OR
(
( DAYOFMONTH( LAST_DAY( NOW() ) ) = DAYOFMONTH( NOW() ) ) --base date is end of month
AND
( DAYOFMONTH(created_at) > DAYOFMONTH( NOW() ) ) --target monthdays are beyond base monthday
)
Incidentally I don't have a MySQL environment, so I'm just taking it on trust that these are the correct functions in MySQL.
Use DAYOFMONTH() function to compare the day of the NOW() and the created_at.
Like this:
SELECT * FROM ORDERS
WHERE (DAYOFMONTH(NOW() < LAST_DAY(NOW()) -- if not last day of month
AND DAYOFMONTH(created_at) = DAYOFMONTH(NOW())
OR (LAST_DAY(NOW()) = DAYOFMONTH(NOW()) -- if last day of month
AND DAYOFMONTH(NOW()) BETWEEN DAYOFMONTH(created_at) AND LAST_DAY(created_at)) --
Inspired by suresubs's answer, I figured out how the query should look like.
SELECT * FROM orders
-- (1)
WHERE (DAYOFMONTH(NOW) < DAYOFMONTH(LAST_DAY(NOW()))
AND DAYOFMONTH(created_at) = DAYOFMONTH(NOW()))
-- (2)
OR (DAYOFMONTH(LAST_DAY(NOW())) = DAYOFMONTH(NOW())
AND DAYOFMONTH(created_at) BETWEEN DAYOFMONTH(NOW()) AND DAYOFMONTH(LAST_DAY(created_at)));
It's using DAYOFMONTH() and LAST_DAY() functions and it's divided into two cases:
Today is not the last day of current month.
Today is the last day of current month.

Selecting the last record of yesterday and all records of today in single query

I know it's possible to get yesterday records, most common way using SUBDATE(CURDATE(), 1) or maybe simply use CURDATE() - 1 and use LIMIT and ORDER to retrieve the last record of yesterday.
But here, I need to get the last record of yesterday in the first row and the rest will be all records of today. I need to run this within single query.
For example, I have following records in one of my table:
--------------------------------------------------
| value | created_at |
--------------------------------------------------
| 70 | 1/1/2017 |
| 300 | 1/1/2017 |
| 100 | 1/1/2017 |
| 235 | 1/2/2017 |
| 45 | 1/2/2017 |
--------------------------------------------------
The created_at column is a timestamp, if today is 1/2/2017 (2th January 2017) then the result of the query should be:
--------------------------------------------------
| value | created_at |
--------------------------------------------------
| 100 | 1/1/2017 |
| 235 | 1/2/2017 |
| 45 | 1/2/2017 |
--------------------------------------------------
So far, I only able to retrieve the records of today with following query:
SELECT * FROM my_table WHERE created_at >= CURDATE();
What query I need to accomplish this?
Hoping you have id as primary key
select * from
(select
*
from
tbl
where date(created_at) =date(DATE_ADD(now(), INTERVAL -1 DAY))
order by id desc limit 0,1
)tmp
UNION
select * from tbl where date(created_at)=date(now())

How to select records form a table with current date and custom time range?

I am having a table with seperate date and time column. I need to select all data from my table checking two condition
1.records with current date(todays date).
2.records with custom time range.
This is my table structure
+------------+------------+---------------------+-------------------+--------------------+
| id | item | description | bill_date | bill_time |
+------------+------------+---------------------+-------------------+--------------------+
| 1 | x | test | 2016-04-15 | 12:05:00 |
+------------+------------+---------------------+-------------------+--------------------+
| 2 | y | test1 | 2016-04-15 | 01:10:44 |
+------------+------------+---------------------+-------------------+--------------------+
| 3 | z | test2 | 2016-04-16 | 05:10:10 |
+------------+------------+---------------------+-------------------+--------------------+
I could select time range using this mysql query
SELECT * FROM `bill_item` WHERE `bill_time` BETWEEN '12:00:00' AND '06:00:00'
this returns 2,3 record now I need to check if the record is todays record. How to do this?
It is possible to have multiple conditions in the WHERE clause. You can use CURRENT_DATE() or NOW() to get only todays entries.
SELECT *
FROM `bill_item`
WHERE (bill_time BETWEEN '12:00:00' AND '06:00:00')
AND bill_date = CURRENT_DATE()
See the official documentation for more Date and Time Functions

UPDATE + SET + WHERE - Dynamic minimum value

This is a follow-up to:
Dynamic minimum value for specfic range (mysql)
I do have the query to fetch the third column (lowest of the last 3 days) "Low_3_days" via SELECT command:
-----------------------------------------
| Date | Unit_ | Lowest_in_last_|
| | price | 3_days |
|----------------------------------------
| 2015-01-01 | 15 | 15 |
| 2015-01-02 | 17 | 15 |
| 2015-01-03 | 21 | 15 |
| 2015-01-04 | 18 | 17 |
| 2015-01-05 | 12 | 12 |
| 2015-01-06 | 14 | 12 |
| 2015-01-07 | 16 | 12 |
|----------------------------------------
select S.Date,Unit_price,
(select S.Date, Unit_price,
(SELECT min(s2.Unit_Price)
FROM table s2
WHERE s2.DATE BETWEEN s.DATE - interval 3 day and
s.DATE - interval 1 day
) as min_price_3_days
FROM table S;
My new challenge is - what is the best way to use UPDATE-SET-WHERE so I could add the ("Lowest_in_last_3_days") values to a new column in a table (instead of having temporary results displayed to me via SELECT).
By following the UPDATE-SET-WHERE syntax, the query would be:
UPDATE table
SET min_price_3_days =
(select S.Date, Unit_price,
(SELECT min(s2.Unit_Price)
FROM table s2
WHERE s2.DATE BETWEEN s.DATE - interval 3 day and
s.DATE - interval 1 day
) as min_price_3_days
but I have difficulties constructing the correct query.
What would be the correct approach to this? I do recognize this one is a tough one to solve.
Your UPDATE should look like:
update table set low_3_days=
(SELECT min(Unit_Price)
FROM (select unit_price, date as date2 from table) as s2
WHERE s2.date2 BETWEEN date - interval 3 day and date - interval 1 day
);
You can check it in SQLFiddle
In Fiddle I used different names for table and column. I prefer not to use SQL keywords as names

Select entries by day over the last 7 days

I want to get the count of the registered users in the past 7 days, grouped.
+-----+------------+--------------+
| id | username | created |
+-----+------------+--------------+
| 1 | Vlad | 1360168194 |
+-----+------------+--------------+
| 2 | Test | 1360168194 |
+-----+------------+--------------+
This is my table. I want to have 7 rows of results with the date of the day, and count(id) as the result for the users that registered.
I tried different solutions and none of them really fitted my needings. Are there any ideas?
SELECT DATE(FROM_UNIXTIME(columName)), COUNT(ID) totalCOunt
FROM tableName
WHERE DATE(FROM_UNIXTIME(columName)) BETWEEN CURDATE() + INTERVAL -7 DAY AND CURDATE()
GROUP BY DATE(FROM_UNIXTIME(columName))
SQLFiddle Demo
Other Source(s)
MySQL Date and Time Functions