首页 > 解决方案 > 如何在 MySQL 查询中使用 MONTHNAME 来匹配月份

问题描述

我正在构建一个查询,以便按月获取新用户列表:

public function getNewUsers($month) {

        $month = date('F', strtotime($month));

        $sql = "SELECT * FROM `{$this->table}` WHERE MONTHNAME(`account_creation_date`) = {$month} ORDER BY `id` DESC";
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute([$month]);

        if ($stmt->rowCount() == 0) return null;

        $users = $stmt->fetchAll(PDO::FETCH_NAMED);

        foreach ($users as &$user) {

            $user = $this->applyRelations($user);

        }

        return $users;

    }

“account_creation_date”值的示例是“08/08/2019 4:55 pm”

我试图传递一个像“August”这样的月份名称,并让查询将 account_creation_date 值转换为它们的月份名称。

尽管我没有收到任何错误,但执行查询时没有返回任何内容。

我怎样才能做到这一点?MONTHNAME 是我想要的吗?

谢谢!

标签: phpmysqlsqlpdo

解决方案


推荐阅读