mysql - DATE in a between dynamic -


i need build sql query following

select  pi.desc,     pa.nameaddress,     pi.ref,     pi.descitem,     pi.quantity,     pi.totaldf,     pi.code,     pi.codebl,     cl.dateship,    po.dtvalidated,     po.supervisordate,  datediff(po.supervisordate, po.dtvalidated) 'diffvalidsupervisor',   datediff(cl.dtlivr, po.supervisordate) 'diffexpevalid',  year(cl.dtlivr),    month(cl.dtlivr)     new.proforma_item pi         inner join     old.cdestk_lig cl on pi.codecde = cl.codcde inner join new.proforma po on po.idproforma = pi.idproforma inner join new.proforma_address pa on po.idproforma = pa.idproforma group pi.desc, pi.ref, pi.descitem, pi.code, pi.codebl, cl.dateship, po.dtvalidated, po.supervisordate, month(cl.dateship), po.dateinvoice having (po.dateinvoice between '2014-01-01' , '2014-12-31') 

but each year have review request change year. want make dynamic, because change manually crazy in our architecture.

the best of is:

say 15 june 2015. between clause covers period:

2015-01-01 2015-05-31

in finite need between take first day of current year , last day of last month.

edit

when in januray, have work on full passed year, not current month. (january treated next month)

you can use date_format function construct first day of year , last_day last day of previous month.

po.dateinvoice between date_format(now() - interval 1 month, '%y-01-01') , last_day(now() - interval 1 month) 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -