【发布时间】:2011-03-13 01:47:38
【问题描述】:
我有一个查询,它返回 3 个字段,其中一个是两位数的月份。
我想基本上拥有它,所以如果月份 == 1 输出 1 月,如果月份 == 02 输出 2 月等
这是我正在尝试的,但这根本不起作用,并阻止整个列在 PHP 中显示。
while ($row = mysql_fetch_array($sqlstr)) {
if ($row['theMonth']=="6") {
echo "<td>{June}</td>";}
echo "<td>{$row['sumSales']}</td>";
echo "<td>{$row['sumPurchases']}</td>";
echo "</tr>";
}
}
什么是做我想做的事情的正确方法,为什么我做错了什么?
我使用的 SQL 查询是:
SELECT theMonth, sum(Sales) as sumSales, sum(Purchases) as sumPurchases
FROM
( SELECT date_format(saledate, '%Y-%m') AS theMonth, sales.cost as Sales, 0 AS Purchases
FROM sales, products
WHERE sales.product=products.name AND category='Food' OR category='Bocas'
OR category='Bebidas' OR category='Flor de cana por botellas'
OR category='Vino por botella' OR category='hora feliz'
UNION ALL
SELECT date_format(purchasedate, '%Y-%m') AS theMonth, 0 as Sales, purchases.cost as Purchases
FROM purchases
) AS all_costs
group by theMonth
我不认为我可以替换
SELECT date_format(purchasedate, '%Y-%m') AS theMonth
与
SELECT MONTHNAME(purchasedate) AS theMonth
那么在 SQL 中以文本形式返回月份名称的最佳方法是什么?
【问题讨论】:
-
你为什么不认为你可以使用
SELECT MONTHNAME(purchasedate)?
标签: php sql mysql conditional if-statement