简体   繁体   中英

mysql procedure CONCAT error

I want copy data to table from another table with current month

This sql is ok
SELECT * FROM aqi_hour where DATE_FORMAT( date, '%Y%m' ) = DATE_FORMAT( CURDATE( ) ,'%Y%m' )


But this is not ok , execute CALL porcedureName() create procedure porcedureName () begin SET @sqlstr = CONCAT(' CREATE TABLE aqi_hour_',DATE_FORMAT(now(),'%Y%m') ,' SELECT * FROM aqi_hour where ', DATE_FORMAT( date, '%Y%m' ) ,'=', DATE_FORMAT( CURDATE( ) , '%Y%m' )); PREPARE stmt1 FROM @sqlstr ; EXECUTE stmt1 ; end create procedure porcedureName () begin SET @sqlstr = CONCAT(' CREATE TABLE aqi_hour_',DATE_FORMAT(now(),'%Y%m') ,' SELECT * FROM aqi_hour where ', DATE_FORMAT( date, '%Y%m' ) ,'=', DATE_FORMAT( CURDATE( ) , '%Y%m' )); PREPARE stmt1 FROM @sqlstr ; EXECUTE stmt1 ; end

Error message : [Err] 1054 - Unknown column 'date' in 'field list'

Thanks!

Try:

SET @`sqlstr` := CONCAT('CREATE TABLE aqi_hour_', DATE_FORMAT(NOW(), '%Y%m'),
                        ' SELECT *
                          FROM aqi_hour
                          WHERE DATE_FORMAT(date, \'%Y%m\') =
                                DATE_FORMAT(CURDATE() , \'%Y%m\')
                        ');

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