djang常用查询SQL语句

原创
2019/01/28 11:08
阅读数 842

将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`
展开阅读全文
加载中

作者的其它热门文章

打赏
1
6 收藏
分享
打赏
2 评论
6 收藏
1
分享
返回顶部
顶部