SQL condition date - mysql

From this database, I want to select those data which payment_date is current month but the service_date is any month except this month.
Database sample

One way to check if 2 dates are in the same month is with the use of the function last_day(), because if for 2 dates the last day of their month is the same then they are in the same month:
select *
from tablename
where last_day(payment_date) = last_day(curdate())
and last_day(service_date) <> last_day(curdate())
If you want the results for any month and not for the current month only:
select *
from tablename
where last_day(payment_date) <> last_day(service_date)

Related

MySQL to get previous year record

I want a MySQL query to fetch previous year records.
I already wrote a query to fetch current year records but I want previous year record also. There is a column called "date_created" based upon this date I have to fetch the status of the meterial.
SELECT material_status, COUNT(*) c
FROM purchase_order
WHERE YEAR(date_created) = YEAR(CURDATE()) AND material_status='open';
to get last year data
SELECT material_status, COUNT(*) c
FROM purchase_order
WHERE YEAR(date_created) = YEAR(DATE_SUB(CURDATE(), INTERVAL 1 YEAR)) AND material_status='open';
Query to see how the date time functions for MySQL works
Try This:
select
CURRENT_USER() user,
CURRENT_TIME() current_time,
CURRENT_DATE() current_date,
year(CURRENT_DATE()) current_year,
YEAR(DATE_SUB(CURDATE(), INTERVAL 1 YEAR)) previous_year;

MySQL select records for the past 3 months

what will be the smartest way to select all rows from MySQL table for the past 3 months if the table has the following columns:
| id (int) | year (int)| month (int) |
Considering that if the current month & year are for example 2.2016 I need to select all records for 11.2015 & 12.2015 & 1.2016
It is easy if the current month is greater than 3 because all months that I need to select are in the same year so I can subtract 3 from the current month and run simple query
SELECT * FROM mytabe where year=2016 and month >= xx
Select * from mytable where STR_TO_DATE(concat(year,"-",month,"-01"),'%Y-%m-%d')>date_sub(curdate(),Interval 3 month) ;
The above query will get fetch year and month from date 3 months before today
You can select three Months records by these queries follow this.
The columnName means which column data want you select.
The tableName means which table data want you select.
The dateColumnName means which column date base you want to select data.
It would return Last Month data from today.
SELECT columName FROM tableName WHERE dateColumName BETWEEN ( DATE(NOW()) - INTERVAL 1 MONTH) AND Date(Now())
It would return Second Last Month data from today.
SELECT columName FROM tableName WHERE dateColumName BETWEEN ( DATE(NOW()) - INTERVAL 2 MONTH) AND ( DATE(NOW()) - INTERVAL 1 MONTH)
It would return Third Last Month data from today.
SELECT columName FROM tableName WHERE dateColumName BETWEEN ( DATE(NOW()) - INTERVAL 3 MONTH) AND ( DATE(NOW()) - INTERVAL 2 MONTH)
May It help to others.
Please try this
Select * from mytable where STR_TO_DATE(concat(year,"-",month,"-01"),'%Y-%m-%d')>date_sub(curdate(),Interval 3 month) ;

Query that displays rows in a range of date with day, month and year

I have the following columns in my table Log:
year, month, day, info
I need a query that selects the rows in a range of date determined by the user. The user will select the initial day, month and year and also the final day, month and year. At the moment, my query is:
SELECT
CONCAT(LPAD(`day`,2, 0),'/',LPAD(`month`,2, 0),'/',`year`) AS data,
info
FROM
Log
WHERE
(year > :initial_year OR (year = :initial_year AND month >= :initial_moth AND day >= :initial_day))
AND (year < :final_year OR (year = :final_year AND month <= :final_month AND day <= :final_day))
GROUP BY
year, month, day
ORDER BY
year DESC, month DESC, day DESC
But this query doesn't display any results, even that they are in the database! What is wrong and how can I fix it?
Your logic is wrong:
WHERE (
year > :initial_year OR (
year = :initial_year AND month >= :initial_moth AND day >= :initial_day
)
)
Will exclude any dates in your initial year where the day portion is greater than the initial day portion. e.g. yyyy-01-31 as the initial day will exclude all results for yyyy where the day portion is not 31.
Similar problems exist with the final date.
As suggested in the comments, use one DATE field in your database and do the other fiddling in your application code; it will save a lot of drama.
If you can't change the database, find and berate the person who designed it until they change it. If you can't do that then:
WHERE (year>:initial_year OR (year=:initial_year AND (month>:initial_month OR (month=:initial_month AND day>=:initial_day))))
and similar for the final date
What about:
select
concat(year,month,day) as thedate, info
from
log
where
thedate >= :startdate and thedate <= :enddate
order by
thedate desc;

Create Date from two cols in mySql

I have a table in my database with two columns: month (1-12) and year (yyyy).
I need to select records between two dates, for exemple
select * from Calendar a where SOMEDATE between STARTDATE and ENDDATE.
So the question is: how can I create the STARTDATE and the ENDDATE from this two columns I have?
...where SOMEDATE between
STR_TO_DATE(CONCAT_WS('-',STARTYEAR,STARTMONTH,1),'%Y-%m-%d')
and
DATE_SUB(
STR_TO_DATE(CONCAT_WS('-',ENDYEAR,ENDMONTH + 1,1),'%Y-%m-%d')
, INTERVAL DAY 1
)
Note that we convert both parts to type date, and use date_sub to subtract a single day from ENDMONTH + 1, since we don't know how many days there are in the relevant month.
You can use this solution to make date from the year and month fields-
SELECT MAKEDATE(year, 1) + INTERVAL month - 1 MONTH FROM calendar;
The apply this one to WHERE condition, e.g. -
SELECT * FROM Calendar a
WHERE
MAKEDATE(year, 1) + INTERVAL month - 1 MONTH BETWEEN
#STARDATE - INTERVAL EXTRACT(DAY FROM #STARDATE) - 1 DAY
AND
#ENDDATE
But I have to ask you about the STARTDATE and ENDDATE criterias. What to do if STARTDATE is '2012-09-20'? Should the query return records with month = 9?

Mysql select next month from a month string column

How can I select records from next month if I have a column called "month" that contains string representations of the actual month names?
SELECT * FROM minutes WHERE year = YEAR(CURDATE()) AND month =
Got it, never mind:
SELECT * FROM minutes WHERE year = YEAR(CURDATE()) AND month = MONTHNAME(now() + INTERVAL 1 MONTH)