【问题标题】:Select a subset of mysql database column - date选择mysql数据库列的一个子集 - 日期
【发布时间】:2017-05-15 06:33:20
【问题描述】:

我有一个问题:

select post_name, post_title, post_date from mysite.wp_posts where post_status='publish'

post_date 输出 GMT 日期和时间,例如:2016-12-17 17:07:26

我想通过 mysql 查询只选择 201612,而不是其余的。

如何修改我的查询来做到这一点?

【问题讨论】:

    标签: mysql substring subset


    【解决方案1】:

    如果您希望它们在单独的列中:

    SELECT post_name,
      post_title,
      YEAR(post_date) YEAR,
      MONTH(post_date) MONTH
    FROM mysite.wp_posts
    WHERE post_status='publish';
    

    如果在同一列中,例如2016-12:

    SELECT post_name,
      post_title,
      date_format(post_date, '%Y-%m') year_month
    FROM mysite.wp_posts
    WHERE post_status='publish';
    

    【讨论】:

    • 真的,真的很有帮助。谢谢。
    猜你喜欢
    • 2012-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 2017-07-19
    • 1970-01-01
    相关资源
    最近更新 更多