【问题标题】:MYSQL join with Group by - wordpressMYSQL 加入 Group by - wordpress
【发布时间】:2020-12-08 13:15:18
【问题描述】:

我在 wordpress 中有一个自定义帖子类型,我想在其中获取按自定义日期字段按月分组的帖子数。我的输出应该是

邮递月数 4月1日 8 月 2 日

到目前为止我的 SQL 代码是

SELECT monthname(met.meta_value), count(pst.id) AS NumberOfPosts
from  wp_postmeta met 
inner join wp_posts pst 
on met.post_id = pst.ID
where pst.post_type = 'custom_post_type'
and met.meta_key = 'start_date'
and pst.post_status = 'publish'
group by meta_value

但是,上面代码的输出是

邮递月数 4月1日 8 月 1 日 8 月 1 日

我做错了什么?

【问题讨论】:

    标签: mysql wordpress


    【解决方案1】:

    将您的 group by 子句更改为 group by monthname(met.meta_value)

    select monthname(met.meta_value), count(pst.id) AS NumberOfPosts
    from  wp_postmeta met 
    inner join wp_posts pst on met.post_id = pst.ID
    where pst.post_type = 'custom_post_type'
        and met.meta_key = 'start_date'
        and pst.post_status = 'publish'
    group by monthname(met.meta_value)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-14
      • 2021-01-13
      • 1970-01-01
      • 2015-05-25
      • 2016-06-11
      • 2012-01-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多