flask_sqlalchemy生成的sql不对

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


回答:

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

以上是 flask_sqlalchemy生成的sql不对 的全部内容, 来源链接: utcz.com/a/158059.html

回到顶部