如何通过从SQLite3 python中的另一个表中选择参数来选择元素

时间:2018-04-02 17:18:20

标签: python python-3.x sqlite flask

是否可以从波兰(country_id = 76)返回城市列表(例如),但是在WHERE中没有直接使用国家名称(country.country行的值)country_id?数据库的结构如下。

@app.route('/citiess')
def city_list3():
    db = get_db()
    data = db.execute('''
        SELECT city FROM city 
        JOIN country ON city.country_id = country.country_id
        WHERE city.country_id = 76
        ''').fetchall()
    data_json = []
    for i in data:
        data_json.extend(list(i))

    return jsonify(data_json)

database

2 个答案:

答案 0 :(得分:1)

为什么不在加入的WHERE表上执行country条款,即:

data = db.execute("""SELECT city FROM city
                     JOIN country USING (country_id)
                     WHERE country.country = ?""", ("Poland", )).fetchall()

答案 1 :(得分:0)

我看到它也有效:

data = db.execute('''
    SELECT city FROM city 
    JOIN country ON country.country_id = city.country_id
    WHERE country.country = "Poland"
    ''').fetchall()

无论如何,谢谢