SQLAlchemy Return All Distinct Column Values

后端 未结 4 2021
时光取名叫无心
时光取名叫无心 2021-02-07 10:45

I am creating a website using Flask and SQLAlchemy. This website keeps track of classes that a student has taken. I would like to find a way to search my database using SQLAlc

4条回答
  •  再見小時候
    2021-02-07 11:06

    As @van has pointed out, what you are looking for is:

    session.query(your_table.column1.distinct()).all(); #SELECT DISTINCT(column1) FROM your_table
    

    but I will add that in most cases, you are also looking to add another filter on the results. In which case you can do

    session.query(your_table.column1.distinct()).filter_by(column2 = 'some_column2_value').all();
    

    which translates to sql

    SELECT DISTINCT(column1) FROM your_table WHERE column2 = 'some_column2_value';
    

提交回复
热议问题