0

Is it possible to return city list (for example) from Poland (country_id = 76) but using country name (value of country.country row) in WHERE not directly country_id? The structure of database bellow.

@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 Answers 2

1

Why don't you just do your WHERE clause on the joined country table, i.e.:

data = db.execute("""SELECT city FROM city
                     JOIN country USING (country_id)
                     WHERE country.country = ?""", ("Poland", )).fetchall()
Sign up to request clarification or add additional context in comments.

Comments

0

I see that also works:

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

anyway thanks

1 Comment

I just parameterized it so you can safely reuse your query for any country you want instead of changing the SQL directly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.