所以我在这里有这么长的查询:
showings = db.execute("SELECT showings.cinema_id, showings.movie_id,\
cinemas.name, movies.name FROM showings JOIN\
cinemas ON showings.cinema_id=cinemas.id JOIN\
movies ON showings.movie_id=movies.id WHERE\
showings.id = :id",
{"id": cinema_id}).fetchall()
return render_template("showings.html", showings2=showings2)
,然后在模板中:
{% for showing in showings2 %}
<option value="{{ showing.id }}">
{{ showing.name }} @{{ showing.name }}
</option>
{% endfor %}
问题是由于两个不同表中的列名相同,当前模板两次返回相同的内容,即Cinema.name的值 是否可以绕过它?
答案 0 :(得分:0)
使用别名重命名列:
showings = db.execute(
"""SELECT showings.cinema_id
, showings.movie_id
, cinemas.name AS cinema_name
, movies.name AS movie_name
FROM showings
JOIN cinemas ON showings.cinema_id=cinemas.id
JOIN movies ON showings.movie_id=movies.id
WHERE showings.id = :id""", {"id": cinema_id}).fetchall()
并在模板中使用上述别名:
<option value="{{ showing.id }}">
{{ showing.cinema_name }} @ {{ showing.movie_name }}
</option>