美文网首页ITBOX
关于Flask-SQLAlchemy中的多关键字过滤查询

关于Flask-SQLAlchemy中的多关键字过滤查询

作者: Cichar | 来源:发表于2017-02-15 21:35 被阅读839次

单字段过滤(某一个键符合即可)

from sqlalchemy import or_

all_results =Model.query.filter(
                            or_(Model.key_1.like("%" + words_1 + "%") if words_1 is not None else "",
                            Model.key_2.like("%" + words_2 + "%") if words_2 is not None else "",
                            Model.key_3.like("%" + words_3 + "%") if words_3 is not None else "",
                            Model.key_4.like("%" + words_4 + "%") if words_4 is not None else "")
                            ).all()

多字段过滤(多个键同时符合)

all_results =Model.query.filter(
                            Model.key_1.like("%" + words_1 + "%") if words_1 is not None else "",
                            Model.key_2.like("%" + words_2 + "%") if words_2 is not None else "",
                            Model.key_3.like("%" + words_3 + "%") if words_3 is not None else "",
                            Model.key_4.like("%" + words_4 + "%") if words_4 is not None else ""
                            ).all()

先根据一个键过滤,然后再单字段过滤

from sqlalchemy import or_

all_results =Model.query.filter_by(id=_id).filter(
                            or_(Model.key_1.like("%" + words_1 + "%") if words_1 is not None else "",
                            Model.key_2.like("%" + words_2 + "%") if words_2 is not None else "",
                            Model.key_3.like("%" + words_3 + "%") if words_3 is not None else "",
                            Model.key_4.like("%" + words_4 + "%") if words_4 is not None else "")
                            ).all()

先根据一个键过滤,然后再多字段过滤

all_results =Model.query.filter_by(id=_id).filter(
                            Model.key_1.like("%" + words_1 + "%") if words_1 is not None else "",
                            Model.key_2.like("%" + words_2 + "%") if words_2 is not None else "",
                            Model.key_3.like("%" + words_3 + "%") if words_3 is not None else "",
                            Model.key_4.like("%" + words_4 + "%") if words_4 is not None else ""
                            ).all()

search_value为单过滤字段(模糊过滤字段),
如果模糊过滤字段为空,则进行精准过滤,
如果精准过滤字段为空,则进行模糊过滤,
如果模糊字段与精准字段都不为空,则先进行模糊过滤,然后对结果再进行精准过滤。

from sqlalchemy import or_

if search_value == '':
    all_results =Model.query.filter_by(id=_id).filter(
        Model.key_1.like("%" + words_1 + "%") if words_1 is not None else "",
        Model.key_2.like("%" + words_2 + "%") if words_2 is not None else "",
        Model.key_3.like("%" + words_3 + "%") if words_3 is not None else "",
        Model.key_4.like("%" + words_4 + "%") if words_4 is not None else ""
            ).all()
elif words_1 == '' and words_2 == '' and words_3 == '' and words_4 == '':
    all_results =Model.query.filter_by(id=_id).filter(
        or_(Model.key_1.like("%" + words_1 + "%") if words_1 is not None else "",
            Model.key_2.like("%" + words_2 + "%") if words_2 is not None else "",
            Model.key_3.like("%" + words_3 + "%") if words_3 is not None else "",
            Model.key_4.like("%" + words_4 + "%") if words_4 is not None else "")
        ).all()
else:
    all_results =Model.query.filter_by(id=_id).filter(
        or_(Model.key_1.like("%" + words_1 + "%") if words_1 is not None else "",
            Model.key_2.like("%" + words_2 + "%") if words_2 is not None else "",
            Model.key_3.like("%" + words_3 + "%") if words_3 is not None else "",
            Model.key_4.like("%" + words_4 + "%") if words_4 is not None else "")
        ).filter(Model.key_1.like("%" + words_1 + "%") if words_1 is not None else "",
                 Model.key_2.like("%" + words_2 + "%") if words_2 is not None else "",
                 Model.key_3.like("%" + words_3 + "%") if words_3 is not None else "",
                 Model.key_4.like("%" + words_4 + "%") if words_4 is not None else ""
        ).all()

相关文章

  • 关于Flask-SQLAlchemy中的多关键字过滤查询

    单字段过滤(某一个键符合即可) 多字段过滤(多个键同时符合) 先根据一个键过滤,然后再单字段过滤 先根据一个键过滤...

  • DataGridView过滤

    1 页面简单布局 2关键字过滤 在TextBox中输入查询关键字,即可。按table_name、comments字...

  • spring data jpa

    1. 过滤数据 ,使用specification-arg-resolver 条件查询2. jpa 关键字 过滤数据...

  • MySQL查询技巧

    1.行转列 2.列转行 3.在子查询中实现多列过滤 单列: 多列: 4.同一属性的多值过滤 使用关联进行查询 使用...

  • MySQL去重distinct

    在MySQL中需要查询表中不重复的记录时,可以使用distinct关键字过滤重复记录。 语法: select di...

  • 2020-03-01

    第7章 商品搜索 学习目标 根据搜索关键字查询 条件筛选 规格过滤 价格区间搜索 分页查询 排序查询 高亮查询 1...

  • Flask框架从入门到精通之模型查询

    1,写一个查询的函数一、查询 其实我们对模型的主要操作就是查询,在Flask-SQLAlchemy中,支持了很多的...

  • MySQL全文索引

    背景概述 通过数值比较、范围过滤等就可以完成绝大多数我们需要的查询,但是,如果希望通过关键字的匹配来进行查询过滤,...

  • Mybatis模糊查询限定词注入

    问题 前端大佬测试反馈,使用MyBatis中的模糊查询时,当查询关键字中包括有_、%时,查询关键字失效,会返回所有...

  • Linux查看日志命令

    查询日志 按行号查看--过滤关键字附近的日志 按日期怎么查呢?按时间段查询的日志 使用more 和less命令 g...

网友评论

本文标题:关于Flask-SQLAlchemy中的多关键字过滤查询

本文链接:https://www.haomeiwen.com/subject/tnfcwttx.html