gpt4 book ai didi

php - 从 mysql 表获取特定月份、php、mysql 的结果

转载 作者:行者123 更新时间:2023-11-29 10:34:18 26 4
gpt4 key购买 nike

我想从 mysql 表中获取特定月份的结果。我编写查询但查询无法正常工作。有人可以帮忙吗?

$sql = "SELECT sum(size) as size FROM tb_size WHERE MONTH(date_created) = '" . mysqli_real_escape_string($db, "Oct") . "' AND YEAR(date_created) =   YEAR(CURRENT_DATE()";
$query = mysqli_query($db, $sql);
$result = mysqli_fetch_object($query);
$size = $result->size;
echo $size;

最佳答案

MONTH() 返回一个数字。所以你的查询应该像这样:

SELECT sum(size) as size
FROM tb_size
WHERE MONTH(date_created) = 10 AND YEAR(date_created) = YEAR(CURRENT_DATE();

如果您希望 Oct 匹配,可以使用 DATE_FORMAT() 代替:

SELECT sum(size) as size
FROM tb_size
WHERE DATE_FORMAT(date_created, '%b') = 'Oct' AND YEAR(date_created) = YEAR(CURRENT_DATE();

关于php - 从 mysql 表获取特定月份、php、mysql 的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46756188/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com