将django语法和sql对应一下,希望对大家有所帮助

查询单个列的值

story.object.values_list("url", flat=True)
SELECT `story`.`url` FROM `hbtc_story` WHERE `story`.`status` = resolved 

AND查询

Stories.objects.filter(Q(status='resolved') & Q(status='developing'))
SELECT * FROM `story` WHERE (`story`.`status` = resolved AND `story`.`status` = developing)

或查询

Stories.objects.filter(Q(status='resolved') | Q(status='developing'))
SELECT * FROM `story` WHERE (`story`.`status` = resolved OR `story`.`status` = developing)

NOT查询

Stories.objects.filter(~Q(status='resolved'))
SELECT * FROM `story` WHERE NOT (`story`.`status` = resolved)

查询为空

Stories.objects.filter(status__isnull=True)
SELECT * FROM `story` WHERE `story`.`plan_id_id` IS NULL

like查询

Stories.objects.filter(status__contains='resolved')
SELECT * FROM `story` WHERE `story`.`status` LIKE BINARY %resolved%;
Stories.objects.filter(status__endswith='resolved')

SELECT * FROM `story` WHERE `story`.`status` LIKE BINARY %resolved;
Stories.objects.filter(status__istartswith='resolved')

SELECT * FROM `story` WHERE `story`.`status` LIKE BINARY resolved%;

in查询

Stories.objects.filter(status__in=('resolved',))
SELECT * FROM `story` WHERE `story`.`status` IN (resolved) ;

distinct查询

story.object.values_list("status", flat=True).distinct()
SELECT DISTINCT `story`.`status` FROM `story`

具体示例代码

Django查询SQL语句

djang常用查询SQL语句的使用代码

# 1 res=models.Book.objects.all()
  # print(res)#<QuerySet [<Book: Book object>, <Book: Book object>, <Book: Book object>, <Book: Book object>, <Book: Book object>, <Book: Book object>]>

  #2 res=models.Book.objects.filter(price='555')
  # print(res)#<QuerySet [<Book: 海贼王>, <Book: 哆啦A梦>]>

  #3 res=models.Book.objects.get(nid=2)
  # print(res.price)#三月的狮子
  # res=models.Book.objects.get(nid=10)
  # print(res)#只能用于一个对象的里面

  #4 res=models.Book.objects.exclude(price='555')
  # print(res)#<QuerySet [<Book: 百变小樱>, <Book: 三月的狮子>, <Book: 水星领航员>, <Book: 火影忍者>]>

  #5 res=models.Book.objects.values('title','publishDate')
  # print(res)#<QuerySet [{'title': '百变小樱', 'publishDate': datetime.date(2000, 8, 8)}, {'title': '三月的狮子', 'publishDate': datetime.date(2017, 10, 19)}, {'title': '水星领航员', 'publishDate': datetime.date(2004, 2, 23)}, {'title': '火影忍者', 'publishDate': datetime.date(1996, 1, 2)}, {'title': '海贼王', 'publishDate': datetime.date(1995, 2, 2)}, {'title': '哆啦A梦', 'publishDate': datetime.date(1969, 12, 31)}]>

  #6 res=models.Book.objects.values_list('title','price')
  # print(res)#<QuerySet [('百变小樱', Decimal('155.00')), ('三月的狮子', Decimal('114.00')), ('水星领航员', Decimal('999.99')), ('火影忍者', Decimal('99.20')), ('海贼王', Decimal('555.00')), ('哆啦A梦', Decimal('555.00'))]>

  #7 res=models.Book.objects.filter(price='555').distinct()
  # print(res)

  #8 res=models.Book.objects.count()
  # print(res) 6

  #9 res=models.Book.objects.first()
  # print(res)#百变小樱

  #10 res=models.Book.objects.exists()
  # print(res)#True

####################################################
  #1 res=models.Book.objects.filter(price__gt=200 )
  # print(res)#<QuerySet [<Book: 水星领航员>, <Book: 海贼王>, <Book: 哆啦A梦>]>
  #
  #2 res=models.Book.objects.filter(price__lte=555,price__gte=114)
  # print(res)#<QuerySet [<Book: 百变小樱>, <Book: 三月的狮子>, <Book: 海贼王>, <Book: 哆啦A梦>]>

  #3 res=models.Book.objects.filter(publishDate__year=2017)
  # print(res)#<QuerySet [<Book: 三月的狮子>]>

  #4 res=models.Book.objects.filter(title__startswith='水')
  # print(res)#<QuerySet [<Book: 水星领航员>]>

  #5 res=models.Book.objects.filter(title__contains='小')
  # print(res)#<QuerySet [<Book: 百变小樱>]>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

风云阁资源网 Design By www.bgabc.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
风云阁资源网 Design By www.bgabc.com

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。