前言
查询的时候经常会用到比较查询,大于某个值或者小于这种。
django里面的比较查询有:
__gt 大于
__lt 小于
__gte 大于等于
__lte 小于等于
__gt 和 __lt
查询表里面年龄大于20的信息
查询sql
SELECT * from yoyo_personinfo WHERE age>20
对应的django查询
python manage.py shell
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from yoyo.models import PersonInfo
>>>
>>> PersonInfo.objects.filter(age__gt=20).values("id","name","age")
<QuerySet [{\'id\': 9, \'name\': \'yoyo5\', \'age\': 21}]>
>>> PersonInfo.objects.filter(age__lt=20).values("id","name","age")
<QuerySet [{\'id\': 7, \'name\': \'yoyo3\', \'age\': 11},
{\'id\': 10, \'name\': \'22\', \'age\': 11},
{\'id\': 11, \'name\': \'bbbbbbbbccccccccc\', \'age\': 11}]>
>>>
__gt 是大于,__lt是小于
__gte 和 __lte
SQL 查询表里面年龄大于等于 20 的信息
SELECT * from yoyo_personinfo WHERE age>=20
对应的django查询
MyDjango>python manage.py shell
>>> from yoyo.models import PersonInfo
>>>
>>> PersonInfo.objects.filter(age__gte=20).values("id","name","age")
<QuerySet [{\'id\': 6, \'name\': \'yoyo2\', \'age\': 20},
{\'id\': 8, \'name\': \'yoyo4\', \'age\': 20},
{\'id\': 9, \'name\': \'yoyo5\', \'age\': 21}]>
>>> PersonInfo.objects.filter(age__lte=20).values("id","name","age")
<QuerySet [{\'id\': 6, \'name\': \'yoyo2\', \'age\': 20},
{\'id\': 7, \'name\': \'yoyo3\', \'age\': 11},
{\'id\': 8, \'name\': \'yoyo4\', \'age\': 20},
{\'id\': 10, \'name\': \'22\', \'age\': 11},
{\'id\': 11, \'name\': \'bbbbbbbbccccccccc\', \'age\': 11}]>
>>>
__gte 是大于等于,__lt是小于等于