如何自己连接表并在SQLAlchemy python中选择列?

时间:2017-07-07 10:23:57

标签: python mysql sqlalchemy

我想在SQLAlchemy python中实现自联接。

我有一张包含event_idevent_nameevent_parent_id的表格。我想从DB中为某些给定的事件ID选择事件ID,事件名称和父事件名称。以下代码没有给出任何结果。 还有一个疑问是如何区分查询结果中的事件名称和父名称。

@app.route("/api/bdi_data_events", methods=["GET"])
def get_bdi_data_events():
  if request.method == "GET":
    try:
        events = request.args.get('events')
        alias1 = BdiDataLifeCycle
        alias2 = BdiDataLifeCycle
        resp = db.session.query(alias1, alias2)\
                .join(alias1.event_parent_id, alias2.event_id)\
                .filter(alias1.event_id.in_(events.split(',')))\
                .values(alias1.event_id, alias1.event_name, alias2.event_name,\
                alias1.event_sequence,\
                alias1.event_reg_dt, alias1.deleted)
        d = []
        print resp
        for i in resp:
            j = {"event_id": i.event_id,"event_name":i.event_name,"event_desc":i.event_desc,"event_name":i.event_name,\
                "event_reg_dt": i.event_reg_dt,"deleted": i.deleted\
                }
            d.append(j)
        return jsonify({"status":"success","response":d})
    except Exception, e:
        print "exception occured-->"
        print e
        return raiseError(BAD_REQUEST, {"message":str(e)})
  else:
    return jsonify({"status":"success"})

等效MySQL查询会给出正确的结果。

SELECT a.event_id, a.event_name, b.event_name as parent FROM bdi_data_life_cycle a 
left join  bdi_data_life_cycle b on 
a.event_parent_id = b.event_id
where a.event_id in (1,2,3,73,83,95,96,97,98)


event_id, event_name, parent
'1', 'data_event', NULL
'2', 'test_event4455', 'data_event'
'3', 'test_event5', 'data_event'
'73', 'test_event10', 'data_event'
'83', 'test_event7', 'data_event'
'95', 'test_event101', 'data_event'
'96', 'test_event100', 'data_event'
'97', 'test_event120', 'data_event'
'98', 'test10', 'data_event'

在这种情况下,事件ID 1是所有剩余事件的父级。

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题,将提供解决方案。我没有使用正确的别名。我使用了错误的连接。我们应该使用label作为列别名。

@app.route("/api/bdi_data_events", methods=["GET"])
def get_bdi_data_events():
  if request.method == "GET":
    try:
        events = request.args.get('events')
        alias1 = aliased(BdiDataLifeCycle)
        alias2 = aliased(BdiDataLifeCycle)
        resp = db.session.query(alias1, alias2)\
                .outerjoin(alias2, alias1.event_parent_id==alias2.event_id)\
                .filter(alias1.event_id.in_(events.split(',')))\
                .filter(alias2.event_parent_id == None)\
                .values(alias1.event_id, alias1.event_name, alias1.event_desc,\
                alias1.event_sequence,\
                alias1.event_reg_dt, alias1.deleted, alias2.event_name.label("event_parent_name"))
        d = []
        print resp
        for i in resp:
            j = {"event_id": i.event_id,"event_name":i.event_name,"event_desc":i.event_desc,"event_sequence":i.event_sequence,\
                "event_reg_dt": i.event_reg_dt,"deleted": i.event_parent_name,"event_parent_name": i.event_parent_name\
                }
            d.append(j)
        return jsonify({"status":"success","response":d})
    except Exception, e:
        print "exception occured-->"
        print e
        return raiseError(BAD_REQUEST, {"message":str(e)})
  else:
    return jsonify({"status":"success"})