Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Flask中,如何使用SQLAlchemy查询我的数据库,以便过滤多个内容?_Python_Flask_Sqlalchemy - Fatal编程技术网

Python 在Flask中,如何使用SQLAlchemy查询我的数据库,以便过滤多个内容?

Python 在Flask中,如何使用SQLAlchemy查询我的数据库,以便过滤多个内容?,python,flask,sqlalchemy,Python,Flask,Sqlalchemy,我在flask中有一个应用程序,用于报告员工的保留率。我想按公司和他们目前是否受雇来细分结果,我想按他们的保留率排序 我想在sql alchemy中复制下面的sql查询,但遇到了麻烦 下面是我想在sql alchemy中创建的sql查询: select * from staffmembers where currentEmployee=1 group by company order by retention desc; 这是我的烧瓶模型: class StaffMember(db.Model

我在flask中有一个应用程序,用于报告员工的保留率。我想按公司和他们目前是否受雇来细分结果,我想按他们的保留率排序

我想在sql alchemy中复制下面的sql查询,但遇到了麻烦

下面是我想在sql alchemy中创建的sql查询:

select * from staffmembers where currentEmployee=1 group by company order by retention desc;
这是我的烧瓶模型:

class StaffMember(db.Model):
    __tablename__ = 'staffMembers'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.Text)
    position = db.Column(db.Text)
    company = db.Column(db.Text)
    startDate = db.Column(db.DateTime)
    endDate = db.Column(db.DateTime)
    retention = db.Column(db.Integer)
    currentEmployee = db.Column(db.Boolean, default=True, nullable=False)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
我当前的sqlalchemy查询是:

staff = StaffMember.query.filter_by(user=current_user).group_by(company).order_by(StaffMember.retention.desc()).all()
问题是,我的报告页面只显示每个公司一个人,而不是该公司所有拥有这些特定数据的人

我的html代码是:

{% for person in staff %} 
    <div class="myform-outercontainer">
        <div  style="width: 650px;">
                <div style="height: 20px"></div>
                        <h3>Current Staff Members:</h3>
        <table>
            <tr>
                <th style="width: 50px" class="my-center">ID</th>
                <th class="my-center">Name</th>
                <th class="my-center">Position</th>
                <th class="my-center">Company</th>
                <th class="my-center" style="width: 100px">Start Date</th>
                <th class="my-center" style="width: 100px">Retention</th>

            </tr>
           <tr>
                <td class="my-center">{{person.id}}</td>
                <td class="my-center">{{person.name}}</td>
                <td class="my-center">{{person.position}}</td>
                <td class="my-center">{{person.company}}</td>
                <td class="my-center">{{person.startDate.strftime("%d/%m/%Y")}}</td>
                <td class="my-center">{{person.retentionNow()}}</td>

            </tr>
        </table>
    </div>
   </div>
{% endfor %}
{%为职员中的人员%}
现任工作人员:
身份证件
名称
位置
单位
开始日期
保留
{{person.id}
{{person.name}
{{person.position}
{{person.company}}
{{person.startDate.strftime(“%d/%m/%Y”)}
{{person.retentionNow()}
{%endfor%}
任何帮助都将不胜感激-D

staff = StaffMember.query.filter_by(user=current_user).group_by(company).order_by(StaffMember.retention.desc()).all()
这个查询将返回所有与当前用户的user col值相匹配的记录,在这种情况下,我猜每个用户只有一条记录

我猜你是在找这样的问题

staff = StaffMember.query.filter_by(currentEmployee=True).group_by(company).order_by(StaffMember.retention.desc()).all()
它提供按currentEmployee筛选、按公司分组并按描述保留率排序的所有员工


你差一点就成功了,只需按逻辑更换过滤器。

没错,我按逻辑塞满了过滤器。我还认为分组比并不是我真正想要的——因为它总是总结出相同的结果。。。这不是我想要的。我将尝试并制定一种不同的过滤方法,以获得更多的结果。我可以看看是否可以将多个筛选条件添加到查询中。谢谢你的帮助!为什么要按公司分组?请参阅:,tl;dr
GROUP BY
将分组行压缩为单个分组行。也许你想按公司下单,而不是保留说明?你是对的,GROUP BY做了一些和我想的不同的事情。谢谢你指出这一点