我在mySQL中使用这行代码:
select sum(amount) as 'October_Amount' from table
where month(date)=month(now());
我希望在“Month_Amount”字符串中获取当前月份(例如“October_Amount”)。我试过这个:
select sum(amount) as monthname(curdate()) from table
where month(date)=month(now());
但它不起作用。 (monthname(now()))
或(monthname(curdate()))
是字符串,因此它应该有效。我尝试了很多版本,没有找到任何建议。
答案 0 :(得分:0)
试试这个:
select sum(amount), monthname(curdate()) from table
where month(date)=month(now());
或者这个:
select sum(amount), CONCAT('AMOUNT_', monthname(curdate())) from table
where month(date)=month(now());
答案 1 :(得分:0)
select CONCAT(sum(amount), '_', month(now())) from table where
month(date)=month(now());
答案 2 :(得分:0)
如果您想使用列名动态,可以像这样使用PREPARED STATEMENTS
SET @date = DATE_FORMAT( CURDATE(),'%M');
SET @query = CONCAT('SELECT SUM(amount) AS "', @date, '_Amount" FROM table where MONTH(date) = MONTH(NOW())');
PREPARE dynamic_statement FROM @query;
EXECUTE dynamic_statement;
DEALLOCATE PREPARE dynamic_statement;