MySQL select Current_timestamp between - mysql

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)

Related

Obtaining max values for each day in the past 28 days

I've seen some of the past posts where there were questions about extracting max values for each day. Based on this, I tried SELECT ts, MAX(current) FROM mytable WHERE ts > date_add(now(), interval -28 day); but I only got one row back which is not what I want.
In this table, ts is my timestamp and current is another column of values. I want to get the max current values for each day in the past 28 days. However, my current values are recorded into the table every 30 mins each day. Therefore, there are many ts values for each day. Could this be the reason why the above query design doesnt work? If so, how can I amend it?
Here is an example of how my table look like for a few rows:
+---------------------+--------------+
| ts | current |
+---------------------+--------------+
| 2018-01-20 16:27:10 | 439.37 |
| 2018-01-20 17:00:18 | 378.07 |
| 2018-01-20 17:30:15 | 204.80 |
| 2018-01-20 18:00:16 | 196.50 |
| 2018-01-20 18:30:16 | 179.40 |
| 2018-01-20 19:00:16 | 183.00 |
| 2018-01-20 19:30:16 | 105.00 |

How to add random interval to timestamp in MySQL?

We got the following table mytable:
+----+------------+------------+
| id | created | expired |
+----+------------+------------+
| 1 | 1496476314 | NULL |
| 6 | 1496477511 | NULL |
| 7 | 1496477518 | NULL |
| 12 | 1496477534 | NULL |
| 13 | 1496477536 | NULL |
| 15 | 1496477541 | NULL |
| 21 | 1496477548 | NULL |
| 22 | 1496477550 | NULL |
| 26 | 1496477565 | NULL |
| 28 | 1496477566 | NULL |
| 29 | 1496477583 | NULL |
+----+------------+------------+
We'd like to do the following:
set expired = created + random(15 - 30 minutes) as unix_timestamp where expired is null;
I currently have no idea to done it.
If u just can give me some ideas it would save my day.
I tried to convert the created timestamp to date_time and add to that date_time the wanted 15 - 30 minutes and finally convert the new_date_time back to unix_timestamp, but there should be an easier way.
If you want to add a random number of minutes between, say, 14 and 33, you can do it like this:
SET expired = DATE_ADD(created, INTERVAL 14 + RAND()*(33-14) MINUTE);
If you want to have seconds granularity, you need to add SECOND-typed intervals:
SET expired = DATE_ADD(created, INTERVAL 14*60 + RAND()*(33-14)*60 SECOND);
This would saves one datetime conversion if you had a DATETIME for the expired column, which makes it slightly easier to expire records (WHERE expired < NOW()). If you have an integer holding a Unix timestamp, then Darshan's answer is definitely the way to go, and you'd do well to calculate the Unix timestamp in your app and then plug it in the query:
WHERE expired <= 123456789
Having an index on that column would make expirations go blazingly fast. I think it might be even faster than the datetime method, but it's just a sensation, I haven't actually checked.
unix_timestamp is number of seconds elapsed since 1st January 1970. Now, if you want to add 15 to 30 minutes then the equivalent seconds would be 900 to 1800. Here's what you can do:
set expired = created + ROUND((RAND() * (900))+900) where expired is null;
This is how it works:
RAND() will generate a random number between 0 and 1
By using RAND() * (maximum - minimum)) + minimum we make sure we generate a number between 900 and 1800.
ROUND then rounds that number to nearest int.

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();

Where clause containing date in MySQL statement not working

Table Name: DemoTable.
Total Fields: 2
Fields:
id (int, auto increment, primary key)
month_and_year (varchar(10))
month_and_year contains date as '2015-03', '2015-01', '2014-12' and so on...
I am trying to get values from the table between '2014-10' and '2015-03'.
SELECT * FROM DemoTable where month_and_year>='2014-10' AND month_and_year<='2015-03' ORDER BY month_and_year DESC
Query does not give desired output as month_and_year field has varchar data type. Changing varchar to date data type isn't possible as date data type does not accept date in 'yyyy-mm' format.
How can the result be obtained?
PS:Is UNIX_TIMESTAMP() a safe bet in this case?
You should never store date value as varchar and choose mysql native date related data types like date,datetime or timestamp
However in your case you need to do some date related calculations before doing the select query. Consider the following table
mysql> select * from test ;
+------+----------------+
| id | month_and_year |
+------+----------------+
| 1 | 2014-10 |
| 2 | 2014-10 |
| 3 | 2014-09 |
| 4 | 2014-11 |
| 5 | 2015-01 |
| 6 | 2014-08 |
+------+----------------+
Now the approach would as
First convert the varchar to real date
Then for the lower limit always start the comparison from first day of the year month value
The upper limit will be till the end of the month.
So the query becomes
select * from test
where
date_format(
str_to_date(
month_and_year,'%Y-%m'
),'%Y-%m-01'
)
>=
date_format(
str_to_date('2014-10','%Y-%m'
),'%Y-%m-01'
)
and
last_day(
date_format(
str_to_date(month_and_year,'%Y-%m'
),'%Y-%m-01'
)
)
<=
last_day(
date_format(
str_to_date('2015-03','%Y-%m'
),'%Y-%m-01'
)
);
The output will be as
+------+----------------+
| id | month_and_year |
+------+----------------+
| 1 | 2014-10 |
| 2 | 2014-10 |
| 4 | 2014-11 |
| 5 | 2015-01 |
+------+----------------+
Use the function STR_TO_DATE(string,format);
http://www.mysqltutorial.org/mysql-str_to_date/
You should use either mysql date time functions or use int field in mysql and store UNIXTIMESTAMP and compare like you are already doing. I think it is overkill to store unixtimestamp because you only need month and year and you won't benefit a lot from unixtimestamp advantages.

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?