【问题标题】:group by months and display chronologically and not alphabetically in mysql按月分组并在mysql中按时间顺序而不是按字母顺序显示
【发布时间】:2013-12-03 05:42:35
【问题描述】:

虽然查询返回结果,但按月份分组按字母顺序返回结果,如 April,August,June ... 但它应该按时间顺序返回结果,如 Jan ,Feb,....

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 

如何修改查询以达到相同的效果?

【问题讨论】:

    标签: php mysql sql date


    【解决方案1】:

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

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-07
      • 1970-01-01
      • 1970-01-01
      • 2010-12-22
      • 1970-01-01
      • 2021-03-27
      • 1970-01-01
      相关资源
      最近更新 更多