数据库
首页 > 数据库> > djang常用查询SQL语句

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`


标签:语句,status,story,djang,objects,SQL,resolved,WHERE,SELECT
来源: http://blog.51cto.com/14009535/2347578