文章内容

2019/10/2 16:37:13,作 者: 黄兵

SQLAlchemy order_by join查询方式

最近需要查询数据库,使用到了外键查询和排序,具体查询代码如下:

get_all = db.session.query(P_List).join(AreaCode).order_by(P_List.id.asc())\
        .paginate(page, per_page=current_app.config['FLASKY_POSTS_PER_PAGE'], error_out=False)

P_List与AreaCode通过外键连接,之后使用P_List.id排序。

最后查询的SQL语句如下:

SELECT P_List.id AS P_List_id, P_List.`PhoneNumber` AS `P_List_PhoneNumber`, P_List.country_flag_id AS P_List_country_flag_id, P_List.group_number AS P_List_group_number, P_List.area_codes AS P_List_area_codes 
FROM P_List 
INNER JOIN phone_number_area_code ON phone_number_area_code.id = P_List.area_codes 
ORDER BY P_List.id ASC


黄兵个人博客原创。

转载请注明出处:黄兵个人博客 - SQLAlchemy order_by join查询方式

分享到:

发表评论

评论列表