按月分组并按时间顺序显示,而不是按字母顺序显示在mysql中

时间:2013-12-03 05:42:36

标签: php mysql sql date

虽然查询返回结果,但按月按月返回结果,如4月,8月,6月...但它应按时间顺序返回结果,如1月,2月,....

SELECT Concat(Monthname(a.fdate), '-', Year(a.fdate)) AS Month, 
       s.new_state AS State, 
       d.new_dist AS District, 
       b.ifbook AS Book, 
       Sum(a.amt) AS Amount 
FROM   str a 
       JOIN sc b 
         ON b.scd = a.isc 
       JOIN USER c 
         ON a.ed = c.str 
       JOIN state_mapping s 
         ON b.state = s.org_state 
       JOIN dist_mapping d 
         ON b.dist = d.org_dist 
WHERE  Trim(b.ifbook) <> '' 
       AND b.ifbook IS NOT NULL 
       AND b.ifbook NOT LIKE '%TR%' 
       AND Trim(d.new_dist) <> '' 
       AND d.new_dist IS NOT NULL 
GROUP  BY b.ifbank, 
          d.new_dist, 
          s.new_state, 
          month 

如何修改查询以实现相同的目标?

1 个答案:

答案 0 :(得分:0)

在不了解您的模型的情况下,我建议您试一试:

select
  concat(monthname(a.fdate),'-',year(a.fdate)) Month,
  s.new_state State,
  d.new_dist District,
  b.ifbook Book,
  sum(a.amt) Amount
from str a
JOIN sc b on b.scd = a.isc
JOIN user c on a.ed = c.str
JOIN state_mapping s on b.state = s.org_state
JOIN dist_mapping d on b.dist = d.org_dist
where TRIM(b.ifbook) <> '' and b.ifbook IS NOT NULL and b.ifbook not like '%TR%'
  and TRIM(d.new_dist) <> '' and d.new_dist IS NOT NULL
group by b.ifbank, d.new_dist, s.new_state, Month
order by year(a.fdate), month(a.fdate)