简体   繁体   中英

Mysql end value of curdate monthname function

Good day, so i know you can create a select statement and views easily, and do a

 select curdate() from accounts  

and then end it as a table name

 select curdate() as demodate from accounts.

But how can i do this.

 select month(curdate()) as monthname(curdate()),
 month(curdate()-1) as monthname(curdate()-1)
 from accounts

All you seem to want is this months number and name and last months number and name so

select month(curdate()), monthname(curdate()),
          month(curdate()) - 1, 
            monthname(date_sub(curdate(), interval 1 month));

+------------------+----------------------+----------------------+--------------------------------------------------+
| month(curdate()) | monthname(curdate()) | month(curdate()) - 1 | monthname(date_sub(curdate(), interval 1 month)) |
+------------------+----------------------+----------------------+--------------------------------------------------+
|               12 | December             |                   11 | November                                         |
+------------------+----------------------+----------------------+--------------------------------------------------+
1 row in set (0.00 sec)

Well here you go.

SET @curdate = CURDATE();
SET @sql = CONCAT('SELECT id AS"', MONTHNAME(@curdate), '"FROM accounts limit 
1');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM