使用flask sqlalchemy在查询语句中对查询结果进行编号

时间:2019-03-13 10:14:51

标签: python mysql flask sqlalchemy

我想给查询的每一行赋予一个排序号,并且我知道MySql环境中的SQL语句。 对于我的测试数据库,无顺序的sql语句是:

SELECT `department`.`name` AS `department`, `staff`.`firstName` AS `firstName`, `staff`.`lastName` AS `lastName` FROM `department`,`staff` WHERE `staff`.`department_id`=`department`.`id`;

它在MySql中的查询结果是:

Query with no number

上述sql语句的相关编号的sql语句为:

SELECT (@i:=@i+1) AS `id`,`department`.`name` AS `department`, `staff`.`firstName` AS `firstName`, `staff`.`lastName` AS `lastName` FROM `department`,`staff`,(SELECT @i:=0) AS `tmp` WHERE `staff`.`department_id`=`department`.`id`;

它在MySql中的查询结果是:

Query with number

我还知道第一个没有数字的情况下python中的flask sqlalchemy语句可以是:

qList=db.session.query(Department.name.label("department"),Staff.firstName.label("firstName"),Staff.lastName.label("lastName")).select_from(Department,Staff).filter(Staff.department_id==Department.id).all()

这会给我一个包含以上数据的列表。

但是如何在烧瓶sqlalchemy语句中编写(SELECT @i:=0) AS tmp " and "(@i:=@i+1) AS id部分?

我使用烧瓶1.0.2,flask_sqlalchemy 2.3.2。

0 个答案:

没有答案