那就先把6个月取出来作为一张子表,再去联其他数据 例如:select * from ( select date_format(DATE_SUB( curdate(),INTERVAL 5 MONTH),'%Y-%m') as d union select date_format(DATE_SUB( curdate(),INTERVAL 4 MONTH),'%Y-%m') as d union select date_format(DATE_SUB( curdate(),INTERVAL 3 MONTH),'%Y-%m') as d union select date_format(DATE_SUB( curdate(),INTERVAL 2 MONTH),'%Y-%m') as d union select date_format(DATE_SUB(curdate(),INTERVAL 1 MONTH),'%Y-%m') as d union select date_format(curdate(),'%Y-%m') as d) as tempmonth left join ......................
SELECT date_format(rp.createAt,'%Y-%m')as investdate, case SUM(rp.fund) when SUM(rp.fund)=null then SUM(rp.fund) else SUM(rp.fund) end SUM(rp.fund), DATE_SUB( last_day(curdate()),INTERVAL 6 MONTH) , last_day(curdate()) FROM r_user_product rp WHERE rp.createAt BETWEEN DATE_SUB( last_day(curdate()),INTERVAL 6 MONTH) AND last_day(curdate()) AND pid = 1 GROUP BY investdate
例如:select * from (
select date_format(DATE_SUB( curdate(),INTERVAL 5 MONTH),'%Y-%m') as d
union
select date_format(DATE_SUB( curdate(),INTERVAL 4 MONTH),'%Y-%m') as d
union
select date_format(DATE_SUB( curdate(),INTERVAL 3 MONTH),'%Y-%m') as d
union
select date_format(DATE_SUB( curdate(),INTERVAL 2 MONTH),'%Y-%m') as d
union
select date_format(DATE_SUB(curdate(),INTERVAL 1 MONTH),'%Y-%m') as d
union
select date_format(curdate(),'%Y-%m') as d) as tempmonth
left join ......................
case SUM(rp.fund)
when SUM(rp.fund)=null then SUM(rp.fund)
else
SUM(rp.fund)
end SUM(rp.fund),
DATE_SUB( last_day(curdate()),INTERVAL 6 MONTH) ,
last_day(curdate())
FROM r_user_product rp
WHERE rp.createAt BETWEEN DATE_SUB( last_day(curdate()),INTERVAL 6 MONTH)
AND last_day(curdate())
AND pid = 1
GROUP BY investdate