I mentioned database table structure below,
name, expired_date, address, payment_date
----------------------
name1, 2013/06/02, address1,2013/07/23
name2, 2013/06/02, address2,2013/07/23
name3, 2013/04/02, address3,2013/07/23
name4, 2013/05/02, address4,2013/07/23
name5, 2013/06/02, address5,2013/07/24
...
name6, 2013/06/02, address6,.....
In this table I update date in yyyy/mm/dd format . current month is April but I need after two records only.
For example I need 06 month records only.
I need SQL query for this.
Assuming you want to query by expired_date:
Following query gets records after two months from current date:
SELECT * FROM tableName where expired_date>=DATE_ADD(CURDATE(), INTERVAL 1 MONTH)
If you specifically want records for June:
SELECT * FROM tableName where expired_date>='2013-06-01' AND expired_date<'2013-07-01'
Try it.
Select [fields] from [yourtable] where Month([yourDateField]) = [Value, ex: for June : 6]
Implement
I am assuming you want to check expired_date
Select * from Table1 where Month(expired_date) = 6
if you want to check year also then, assuming year is 2013.
Select * from Table1 where Month(expired_date) = 6 and Year(expired_date) = 2013
Related
Re-do Times 2 for clarity
For simplicity and clarity, the data I am aggregating is in a database I will refer to as "BaseA".
Normally, when comparing Month over month data, I can use the following query:
select date_trunc('month',hour) as date,
sum(a) as total_a,
sum(b) as available_b,
sum(c) as c,
sum(d) as net_d
from BaseA where id=12345 and hour >='2022-01-01'
group by date order by date desc
Instead of looking back and collecting ALL months from 2021-2022 for the duration I wish to view, I want to collect ONLY two months of data, those being the following:
October 2021
vs.
April 2022
I'd like the data to be visualized in the Month over month format, like so:
example
However, I would like to:
Select all BaseA columns (aka, select *)
Only include Two rows: April 2022 & October 2021
So, should come out like so:
example 2
This query is what Im trying to do (in word form since I can't write it)
Select *
from BaseA
where date
is in
April 2022
&
October 2022
The result of the above should result in 2 rows of data (one for each month referenced)
Is there a place in the below query where DISTINCT would make that actualized?
select * from BaseA
where id=12345 and
(
(month(month) = '04' and year(month) = '2022')
OR
(month(month) = '10' and year(month) = '2021')
)
--DISTINCT go where?
Appreciate the help!
To get data for the two months try something like this:
select * from BaseA
where _id=12345 and
(
(month(month) = '04' and year(month) = '2022')
OR
(month(month) = '10' and year(month) = '2021')
)
I have no idea what you mean by "return data consolidated (grouped) by month (so multiple lines occur per month)". Can you provide dummy data that illustrates the data that you have and the result that you want to achieve?
Say that there is a MySQL table which has a column with data type datetime and there are many rows with year 2015,2016,2017... at this column.
The problem is that i want to write a query that finds the row with highest(newest) datetime column for a specific year. For example i want the query to find the row of highest datetime value for 2015. (I assumed that 31 December is higher than 30 December).
You can do:
select t.*
from t
where t.datetimecol = (select max(t2.datetimecol)
from t t2
where year(t2.datetimecol) = year(t.datetimecol)
);
SELECT MAX(mydate)
FROM mytable
WHERE YEAR(mydate) = 2017
or
SELECT YEAR(mydate), MAX(mydate)
FROM mytable
GROUP BY YEAR(mydate)
select * from table where 1 and DATE_FORMAT(dateColumn,"%d-%m") BETWEEN "01-09" and "30-09"
This query is returning all the data. I just want the data between these dates, without taking into account the year.
In the table dateColumn is yyyy-mm-dd.
You need to do the comparison as MM-DD, not DD-MM:
select *
from table
where DATE_FORMAT(dateColumn, '%m-%d') BETWEEN '09-01' and '09-30'
Or, for this case, you could just do:
where month(dateColumn) = 9
Try this:
SELECT *
FROM table
WHERE MONTH(dateColumn) = 9
You can check if the month is September
select * from table where DATE_FORMAT(dateColumn,"%m")
= '09
If you want rows from your table where the month of your date is 9 and the day of month is between 1 and 30, you can actually do this quite easy and more explicit:
SELECT
*
FROM
table t
WHERE
MONTH(t.date) = 9 and
DAY(t.date) BETWEEN 1 and 30
It's also easier to understand.
I am looking for query which fetch record between two months.
It looks simple but I am looking for a query which fetch record for two different months.
ie.
January and April
query should show result from the month of January and April but it should not show result for February and march.
Try
SELECT * from table_name where month(date_field) = 1 or month(date_field) = 4
this will show all results for january and april, but for all years. to filter year, add another check in query
SELECT * from table_name where (month(date_field) = 1 or month(date_field) = 4) and year(date_field) = 2016
SELECT * FROM data where (data.createdAt BETWEEN "2016-01-01" AND "2016-01-31") OR (data.createdAt BETWEEN "2016-04-01" AND "2016-04-31")
I have a table which contains date (Field Type: Date and Date Format: %Y-%m-%d) as a field. I need to select all the rows from the table for all the years whose date is not between Dec 3rd and Dec 24th.
The table contains month and day as a separate fields.
The result can be obtained by using the following query:
select * from mytable where date not in (select date from mytable where month=12 and day between 3 and 24);
But i m trying to get the result in a single query like the below one but it gave empty rows:
select * from mytable where date not between '%Y-12-03' and '%Y-12-24';
Can it be done in a single query like the above one?
SELECT *
FROM mytable
WHERE MONTH(`date`) <> 12
OR DAY(`date`) NOT BETWEEN 3 AND 24
;
This will give you every row that meets the requirements. I'm sure someone has a faster way of doing this, since this will ignore all indexes and will likely be slow on a large dataset, but it does work and return the data you require, so if no-one can suggest an improvement this will answer your question.