flask_sqlalchemy生成的sql不对

新手上路,请多包涵

这是我的代码

today_user = db.session.execute("""select sum(money) from tb_recharge 
where state = 1 and date_format(create_time,'%Y') = '2021'""")

输出的结果

2021-03-25 15:54:31,726 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'sql_mode'
2021-03-25 15:54:31,727 INFO sqlalchemy.engine.base.Engine ()
2021-03-25 15:54:31,732 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'lower_case_table_names'
2021-03-25 15:54:31,733 INFO sqlalchemy.engine.base.Engine ()
2021-03-25 15:54:31,754 INFO sqlalchemy.engine.base.Engine SELECT DATABASE()
2021-03-25 15:54:31,754 INFO sqlalchemy.engine.base.Engine ()
2021-03-25 15:54:31,761 INFO sqlalchemy.engine.base.Engine show collation where `Charset` = 'utf8mb4' and `Collation` = 'utf8mb4_bin'
2021-03-25 15:54:31,761 INFO sqlalchemy.engine.base.Engine ()
2021-03-25 15:54:31,769 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS CHAR(60)) AS anon_1
2021-03-25 15:54:31,769 INFO sqlalchemy.engine.base.Engine ()
2021-03-25 15:54:31,777 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS CHAR(60)) AS anon_1
2021-03-25 15:54:31,777 INFO sqlalchemy.engine.base.Engine ()
2021-03-25 15:54:31,783 INFO sqlalchemy.engine.base.Engine SELECT CAST('test collated returns' AS CHAR CHARACTER SET utf8mb4) COLLATE utf8mb4_bin AS anon_1
2021-03-25 15:54:31,783 INFO sqlalchemy.engine.base.Engine ()
2021-03-25 15:54:31,797 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2021-03-25 15:54:31,797 INFO sqlalchemy.engine.base.Engine select sum(money) from tb_recharge 
                    where state = 1 and date_format(create_time,'%%Y') = '2021'
2021-03-25 15:54:31,797 INFO sqlalchemy.engine.base.Engine ()

会多出来个%,很奇怪,试过了‘%%Y’,不行,
数据库版本是 mysql5.7.17

回复
阅读 1.2k
1 个回答

为何多个%,换个写法试试 create_time BETWEEN '2021-01-01 00:00:00' AND '2021-12-31 23:59:59'Year(create_time) = 2021

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏