django数据库查询条件
djang常用查询SQL语句的使用代码将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语句
|
# 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: 百变小樱>]> |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持开心学习网。
原文链接:https://www.cnblogs.com/ldq1996/p/7737827.html