selecting data from mysql table based on a date column and condition - mysql

I have a column in mysql table PACKAGES as SERVICE_EXPIRY_DATE which holds a date time value.
Now I want to match only those records whose SERVICE_EXPIRY_DATE is coming within the next 15 days from the moment of querying.
How can I match those columns.

You could do it by using intervals
WHERE `SERVICE_EXPIRY_DATE` BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 15 DAY)
Look at MySql documentation for more reference https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html

Related

Get last 3 months record from paid salary table where month and year is stored in separate column

I need help with the MySQL query. I have a table like this in MySQL
What I want is to get the last 3 months' records from the table. But I don't understand how to make this MySQL query.
I have tried to contact both columns and tried to generate a MySQL date to compare but it returns me null.
I also tried this query and it works for current data. But i am not sure if it is the only way to do this or is there any better way.
What I want is to get the last 3 months' records from the table.
I think you want a where clause like this:
date(concat_ws('-', salary_year, salary_month, 1)) >= (curdate() - interval (1 - day(curdate()) day) - interval 2 month
The key idea here is to convert the string to a date so it is handled correctly.
If you have a fixed date, then you would represent that as:
date(concat_ws('-', salary_year, salary_month, 1)) >= '2021-04--01'

how to convert from one date format to another in mysql

I am using MySQL and I want to get all the dates in the previous week from a column, say xyz, which is of type date and has the format YYYY-MM-DD. However, when I use
select
*
from
tablename
where
xyz > date_sub(curdate(),INTERVAL 1 week);
I get dates which are not within the past 1 week. I get dates which are one month from now and some random dates.
You query is pulling all records in which xyz (your date column I assume) is newer than one week ago. This could in fact be future records. If you want to exclusively view things from the previous week, you need to use the between keyword.
select
*
from
tablename
where
xyz between date_sub(curdate(), INTERVAL 1 week) and curdate()

Unable to create computed field with date_add in SQL

In my database, I have a table called 'fine', in that table I have three fields, issue_date, expiry_date and fine_amount. I want the expiry_date to be computed from the issue date. The expiry date should always have 20 days more than the issue_date, So I wrote the query as:
ALTER TABLE fine ADD
expiry_date AS DATE_ADD(CURRENT_DATE,INTERVAL 20 DAY)
But there is a syntax error. I can't seem to find the solution.
Also I want the fine_amount to be 10 * (difference in days between current date and expiry date if current days exceeds expiry date). How do I go about doing that?
You can't implement the fine logic using a computed column because the formula involves the current time which is non deterministic. From the MySQL documentation:
Literals, deterministic built-in functions, and operators are permitted. A function is deterministic if, given the same data in tables, multiple invocations produce the same result, independently of the connected user. Examples of functions that fail this definition: CONNECTION_ID(), CURRENT_USER(), NOW().
So your best bet probably is to just compute values for these columns at the time you actually select. For example:
SELECT issue_date,
DATE_ADD(issue_date, INTERVAL 20 DAY) AS expiry_date,
CASE WHEN NOW() > DATE_ADD(issue_date, INTERVAL 20 DAY)
THEN 10*DATEDIFF(NOW(), DATE_ADD(issue_date, INTERVAL 20 DAY))
ELSE 0 END AS fine_amount
FROM fine

Checking time-stamps if they are from today in MySQL

I have a database with several tables. Each of these tables has a column 'created' which contains time-stamps of when that particular row was created in the database.
Now, I want to create a MySQL script that checks once every week if there is data coming into these tables. So, there should be data coming everyday. How do I create a MySQL script that allows me to do this for all the tables in the database?
Note: Remember I want to do this for all the tables in the database with a single script. That's the main thing I want to know.
i use this approach for a table called call, with a column of timestamp type called systemdate:
SELECT * FROM `call` WHERE DATE(`systemdate`) = DATE(NOW());
mysql DATE() statement gets the datepart of a datetime or timestamp field.
Sorry, just noticed that you want to check if atleast there is an entry for each of the days in the previous week.
you can use this query to check the prevous days individually:
yesterday:
SELECT * FROM `call` WHERE DATE(`systemdate`) = DATE(NOW()) - 1;
before yesterday:
SELECT * FROM `call` WHERE DATE(`systemdate`) = DATE(NOW()) - 2;
Or you can check the whole week at once:
SELECT * FROM `call` WHERE DATE(`systemdate`) > DATE_SUB(CURDATE(), INTERVAL 7 DAY) GROUP BY DATE(`systemdate`);
This will return one result for each day, so if you have 7 results you'll know at least an entry was made on each day.
select * from table
where created between subdate(current_date, interval 7 day) and current_date;
Selecting datetimes up to current_date includes everything up to the start of "today" (ie "the previous midnight").

Check given date less than current_date

I have expiry date I want to check the Expiry date is less than current or not. and if the Expiry date is ex:27-03-2011. I want to do some operation if the expiry is ex:27-04-2011 no need to do anything.
I want to check and report the details for only one month,
after one month of the expiry date I don't want to check
how to do in query?
table1
field type
expdate date
Ex:2012-01-20
Please use MySQL date and time formats, they have the form YYYY-MM-DD (ISO date format). All MySQL Date and Time functions http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html work using this format.
MySQL also has date and time arithmetic. You can write expressions such as "NOW() - INTERVAL 5 day", which is also explained here http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add
root#localhost [(none)]> select now(), now() - interval 5 day\G
*************************** 1. row ***************************
now(): 2011-03-28 13:49:24
now() - interval 5 day: 2011-03-23 13:49:24
1 row in set (0.00 sec)
To list all items older than 5 days, use
SELECT id FROM sometable WHERE created > NOW() - INTERVAL 5 DAY
or a similar query. There are a few things to take note of:
an index on created should exist to make this fast
the column name created is not part of an expression in order to enable it to be used with an index. We are specifically writing 'created > NOW() - INTERVAL 5 DAY' and not 'created + INTERVAL 5 DAY > NOW()' - this expression uses the column name created in an expression, so no index usage possible.
if you are using this with a delete query, you are basically deleting old data from the left hand side of the time arrow, inserting new data at the right hand side of the time arrow. Maybe you want to use MySQL PARTITIONS and then PARTITION BY RANGE your table. That would allow you to delete old data by using ALTER TABLE DROP PARTITION throwing away the partition for say 5 days ago, creating a new one for tomorrow. This is much faster than DELETE in many cases.