zoukankan      html  css  js  c++  java
  • 字段查询谓词表

    Django 字段查询谓词表

     

    Django 字段查询谓词表

    谓词 含义 示例 等价SQL语句
    exact 精确等于 Comment.objects.filter(id__exact=14) select * from Comment where id=14
    iexact 大小写不敏感的等于 Comment.objects.filter(headline__iexact=’I like this’) select * from Comment where upper(headline)=’I LIKE THIS’
    contains 模糊匹配 Comment.objects.filter(headline__contains=’good’) select * from Comment where headline like “%good%”
    in 包含 Comment.objects.filter(id__in=[1,5,9]) select * from Comment where id in (1,5,9)
    gt 大于 Comment.objects.filter(n_visits__gt=30) select * from Comment where n_visits>30
    gte 大于等于 Comment.objects.filter(n_visits__gte=30) select * from COmment where n_visits>=30
    lt 小于    
    lte 小于等于    
    startswith 以…开头 Comment.objects.filter(body_text__startswith=”Hello”) select * from Comment where body_text like ‘Hello%’
    endswith 以…结尾    
    range 在…范围内 start_date=datetime.date(2015,1,1)
    end_date=datetime.date(2015.2.1)
    Comment.objects.filter(
        pub_date__range=(start_date,end_date)
    )
    select * from Comment 
    where pub_date
    between ‘2015-1-1’ and ‘2015-2-1’
    year Comment.objects.filter(
        pub_date__year=2015
    )
    select * from Comment 
    where pub_date
    between ‘2015-1-1 0:0:0’ 
    and ‘2015-12-31 23:59:59’
    month    
    day    
    week_day 星期几    
    isnull 是否为空 Comment.objects.filter(
        pub_date__isnull=True
    )
    select * from Comment where
    pub_date is NULL

    filter(**kwargs): 返回符合筛选条件的数据集

    exclude(**kwargs):   返回不符合筛选条件的数据集

    Comment.objects.filter(pub_date__year=2015)

    多个filter和exclude可以链接在一起查询

    Comment.objects.filter(pub_date__year=2015).exclude(pub_date__month=1).exclude(n_visits__exact=0)

    get() :查询单条记录,注意没有查询到数据的时候会报错

    Comment.objects.get(id_exact=1)

    all():  查询所有数据

    Comment.objects.all()
    Comment.objects.all()[:10]
    Comment.objects.all[10:20]

    order_by():   排序

    Comment.objects.order_by('headline')
  • 相关阅读:
    [一个经典的多线程同步问题]解决方案二:Event事件
    [进程与线程]进程、线程的生活
    从C++strStr到字符串匹配算法
    Dubbo 服务集群容错配置
    TCP三次握手
    zookeeper 学习 客户端Acl操作笔记
    ubuntu下的“用vim打开中文乱码,用cat打开正常显示”的解决方法
    JDK1.7 Update14 HotSpot虚拟机GC收集器
    账户信息不存在的问题
    win10 64 使用 visual studio 2017 搭建汇编开发环境
  • 原文地址:https://www.cnblogs.com/ls13691357174/p/9621891.html
Copyright © 2011-2022 走看看