用flask和sqlalchemy选择两个不同的列

时间:2017-04-24 21:51:15

标签: sql python-2.7 flask-sqlalchemy

这是我的第一个Flask项目,我正在努力坚持MVC而不仅仅是开始编写证明难以实现的SQL。将这个简单的查询转换为Flask-SQLAlchemy是我目前无法解决的难题。

SELECT DISTINCT gradelevel, beginningorendtest 
FROM wordlist 
ORDER BY gradelevel,beginningorendoftest

我很欣赏任何想法,Flask-SQLAlchemy可能无法实现。这是我最近的尝试。

data = wordlist.query.add_columns(wordlist.gradelevel, wordlist.beginningorendtest).distinct()
print (data)    
for row in data:
    print ("Gradelevel:" + row.gradelevel + " BeginningorEndoftest:" + row.beginningorendtest)

1 个答案:

答案 0 :(得分:0)

经过一段时间的修改后,这个:

 data = wordlist.query.with_entities(wordlist.gradelevel, wordlist.beginningorendtest)
        .distinct().order_by(wordlist.gradelevel).order_by(wordlist.beginningorendtest)

将生成此等效查询:

 SELECT DISTINCT wordlist.gradelevel AS wordlist_gradelevel,wordlist.beginningorendtest AS wordlist_beginningorendtest 
 FROM wordlist ORDER BY wordlist.gradelevel, wordlist.beginningorendtest