Date-wise repeat query - mysql

I have a table in my application which stores the user details. In those fields, user's saving their alert settings. I.e. we will alert the users on their saved date (s).
My table looks like
id | Username | name | alert_date | repeat_on
--------------------------------------------------------------------------------
12 | user_name | name | 2013-08-12 00:00:00 | 30
15 | user_nam2 | name | 2013-05-12 00:00:00 | 45
--------------------------------------------------------------------------------
With a cronjob we are sending reminder email and SMS to the users. Like
select * from users where date(alert_date)=CURRENT_DATE
It's working fine but there is an option to repeat the alert according to "repeat_on" days. Like we have to repeat the alert on each '30' days (or whatever) from the alert_date. I am stuck here with the query.
Is this the correct structure of the table to do the same?

Try:
SELECT * FROM users
WHERE alert_date <= CURRENT_DATE AND DATE_ADD(alert_date, INTERVAL repeat_on DAY) >= CURRENT_DATE

Related

Show only available time slots in select field

I am working on a booking system where users can book certain services online. I am stuck on finding and displaying available time slots within a specific day. I know the length of the needed time slot is 1 hour and the business hours.
Is there a way to show time slots that has not yet been booked on a certain day and display only the available time slots that is available to be booked in a dropdown select form?
If a customer selects a specific day and clicks "Select Day" then it needs to query the DB and return the results.
My SQL structure is as follows
|id | title | start_time | end_time | booking_date |
| 1 | Name1 | 2022-05-12 08:00:00 | 2022-05-12 09:00:00 | 2022-05-12 |
| 2 | Name2 | 2022-05-12 10:00:00 | 2022-05-12 11:00:00 | 2022-05-12 |
| 3 | Name3 | 2022-05-12 13:00:00 | 2022-05-12 14:00:00 | 2022-05-12 |
| 4 | Name4 | 2022-05-12 14:00:00 | 2022-05-12 15:00:00 | 2022-05-12 |
as per above the select form should display the timeslots that is not already taken.
09:00 - 10:00
12:00 - 13:00
15:00 - 16:00
It would be something like:
select
id, title
from
<table>
where
start_time between '2022-05-12 00:00:00' and '2022-05-12 11:59:59'
and
booking_date is null
I don't know the name of your table, so you would need to replace <table> with that. I'm also assuming that "booking_date" will have a value to indicate that time slot has been reserved, that it's a date field, and it will be null if that slot hasn't been selected. However, booking_date could have a different purpose.
This is a lazy answer
(because I think just use SQL will do it, use subSelect and other function, but I don't know how to do, sorry.)
get today occupy time:
SELECT id, TIME(start_time) AS s_time FROM tablename
WHERE start_time >= '2022-05-12 00:00:00'
AND start_time < '2022-05-13 00:00:00'
diff time in php:
$sqlResult = []; // sql result
$timeAll = [
'00:00:00',
'01:00:00',
'02:00:00',
'03:00:00',
... // TODO: we need fill it
'23:00:00',
];
foreach ($sqlResult as $item) {
if (isset($timeAll[$item['s_time']])) {
unset($timeAll[$item['s_time']]);
}
}
return $timeAll;
// TODO: javascript or other client code can use it.
ref knowledge link:
MySQL SELECT WHERE datetime matches day (and not necessarily time)
If you choose 2022-05-26, and Peter is occupying room F25 from 2022-05-16 until 2022-05-29, it means the date you select must be out of this range.
So, the query below will only return rooms that were not booked on that day.
SELECT b.id, b.room_id as available_room FROM booking as b
WHERE(
unix_timestamp('$mydate')
NOT BETWEEN unix_timestamp(b.start_date)
AND unix_timestamp(b.end_time)
)
AND unix_timestamp(b.end_time) < unix_timestamp('$mydate');
Assuming $mydate is the variable that contains the date selected by the user, the above query will return rooms that will be available in the future on that particular day.

Select all records from MySQL table where specified time is reached or passed

I have a table where I insert jobs that my user posts. I have a job_expiration column where I'm adding the job expiration time with
now() + INTERVAL some DAY like so.
I want to run a cron job to set the job status to late where the job_expiration is reached.
I have tried doing it SELECT * FROM jobs WHERE status = 'active' AND job_expiration < NOW() like so.
but it didn't worked.
here is how table looks like:
+----+------------+---------------------+--------+
| id | title | job_expiration | status |
+----+------------+---------------------+--------+
| 1 | something | 2020-05-04 09:31:24 | active |
| 2 | something2 | 2020-06-01 06:06:58 | active |
+----+------------+---------------------+--------+
I want to select first row here.
how can I do that?

How to get the expired candidates in MySQL

I am having a table with the columns for expired_date and registered_date.
Expired date have set for 2 days to registered date.
Its look like this:
+--------------+--------------+---------------------+
| candidate_id | date_expires | date_added |
+--------------+--------------+---------------------+
| 1 | 2016-03-26 | 2016-03-24 14:42:18 |
| 2 | 2016-03-23 | 2016-03-21 15:43:40 |
| 3 | 2016-02-15 | 2016-02-13 14:53:30 |
| 4 | 2016-02-22 | 2016-02-20 14:54:19 |
+--------------+--------------+---------------------+
My question is, I want to select expired profile to current date and time.
This is how I tried it, but it doesn't work.
SELECT * FROM candidates WHERE date_added = DATE_ADD(date_added, INTERVAL 2 DAY);
Hope somebody may help me out.
Thank you.
You may try any of the following query which meets your need.
SELECT
*
FROM candidates
WHERE date_expires < CURDATE();
Or if you want to get the expired accounts with respect to date_added field then follow the query given below:
SELECT
*
FROM candidates
WHERE DATE_ADD(date_added, INTERVAL 2 DAY) < CURDATE();
EDIT:
For fine-grained comparison you may use the following query:
SELECT
*
FROM candidates
WHERE TIMESTAMPADD(DAY,2,date_added) < NOW();
Note: Actually you don't need to store the expired dates in database. Rather you can store the profile life time (in this case it is 2 Days) in database if this profile life time varies across different accounts. You don't need to store this in database if it's constant in nature (i.e. Always 2 DAYS).
So if you want to bring this change in your table structure then it would look like below:
+--------------+--------------+---------------------+
| candidate_id | days | date_added |
+--------------+--------------+---------------------+
| 1 | 2 | 2016-03-24 14:42:18 |
| 2 | 5 | 2016-03-21 15:43:40 |
| 3 | 3 | 2016-02-13 14:53:30 |
| 4 | 10 | 2016-02-20 14:54:19 |
+--------------+--------------+---------------------+
You need a modified query for this change.
Here it is:
SELECT
*
FROM candidates
WHERE TIMESTAMPADD(DAY,days,date_added) < NOW();
You're looking for this
SELECT *
FROM candidates
WHERE date_expires < NOW();

MySQL select Current_timestamp between

I have a list of users in MySQL and on subscription the timestamp is set in the data base using the CURRENT_TIMESTAMP.
Now I want to do a select from this table where the subscribe date is between day X and day Y
I tried several queries but somehow they all turn up empty.
Here is my last version
SELECT *
FROM users
WHERE subscribe_date BETWEEN '2013-10-07'AND '2013-13-10'
As I know for sure this date: 2013-10-08 14:38:49
is in the subscribe_data field It should turn up somehow
What is the best way to do this?
Maybe good to know my 'subscribe_date' column has type 'timestamp' and is auto filled with 'CURRENT_TIMESTAMP'
Here is the data in this table:
+----+-----------+---------------------+
| id | firstname | subscribe_date |
+----+-----------+---------------------+
| 20 | Peter | 2013-10-01 14:37:17 |
| 21 | Jack | 2013-10-08 14:38:49 |
| 22 | Andrew | 2013-10-10 14:41:03 |
| 23 | Margret | 2013-10-14 14:42:46 |
+----+-----------+---------------------+
Since TIMESTAMP is up to seconds precision usually, you have to add time part:
SELECT *
FROM users
WHERE (subscribe_date BETWEEN '2013-10-07 00:00:00' AND '2013-12-10 23:59:59')
I've fixed your '2013-13-10' to '2013-12-10 23:59:59' since there's no 13-th month (and in DATETIME format it's YYYY-MM-DD, so month goes second)

mysql datetime range select

I have a recurring payment mode in the system. All is well, API successful response is logged on the database.
The concern of my question is, I'm about to automate the subsequent payment action (which unfortunately, the payment gateway doesn't support) of the system.
Via cron I'll schedule a check on whether which accounts must be included in the subsequent process and notice.
I have here a snip of the database entry:
+---------------------+---------------------+--------------------+
| payment_date | payment_expirydate | transaction_number |
+---------------------+---------------------+--------------------+
| 2012-02-14 03:47:15 | 2012-05-14 03:47:15 | 1-67815163 |
| 2012-02-16 00:53:03 | 2012-05-16 00:53:03 | 1-69010235 |
| 2012-02-16 08:57:16 | 2012-05-16 08:57:16 | 1-69027483 |
| 2012-02-16 09:08:06 | 2012-05-16 09:08:06 | 1-69027694 |
| 2012-02-16 09:58:17 | 2012-05-16 09:58:17 | 1-69028921 |
| 2012-02-17 09:28:32 | 2012-05-17 09:28:32 | 1-69072076 |
| 2012-02-17 06:17:45 | 2012-05-17 06:17:45 | 1-69068200 |
| 2012-02-17 11:12:08 | 2012-05-17 11:12:08 | 1-69074788 |
+---------------------+---------------------+--------------------+
I am having a difficulty in creating the SQL query for this. Assuming, the date today is 2012-05-16 and the time is 07:00:00. I want to get all the accounts which is today and less than the current time. For instance, the only valid account (based on the current date and time I indicated) I need is account 1-69010235.
Also, any tips if on what interval should I set my cron to run?
This query will return all records that expire today -
SELECT *
FROM accounts
WHERE payment_expirydate BETWEEN CURRENT_DATE AND (CURRENT_DATE + INTERVAL 1 DAY - INTERVAL 1 SECOND)
If you want all accounts expiring today but less than current time -
SELECT *
FROM accounts
WHERE payment_expirydate BETWEEN CURRENT_DATE AND CURRENT_TIMESTAMP
For codeigniter's AR implementation you should be able to use -
$this->db->where('payment_expirydate BETWEEN CURRENT_DATE AND CURRENT_TIMESTAMP', NULL, FALSE);
SELECT group_concat(transaction_number) as transaction_number
FROM tab
WHERE payment_date BETWEEN CURRENT_DATE AND CURRENT_TIMESTAMP
You can use this to return a string and use perl to split the string and pass to curl.
sqlfiddle here.
If you want all the records in the table you show that are valid for the current time - and valid means that the payment_date is earlier than now and payment_expirydate is later than now, you can use:
SELECT transaction_number from `table`
WHERE payment_date > now()
AND payment_expirydate < now();
Does that get what you want?