Django Query:如何使用sql“union”和“not in”函数

时间:2021-01-05 19:17:58

How can use union and "not in" function in django query. I have searched on it but cannot find any example

如何在django查询中使用union和“not in”函数。我搜索过但却找不到任何例子

select id,address
    from tbl_nt
    where st_id in (1,2) and name = 'foo'
    union
    (select d.id,d.addrses from tbl_nt_123 d
    where d.name='foo' and condition_id not in (select condition_id 
    from tbl_conditions 
    where version_id = 5)

i have tried this query for lower portion but didn't work

我已经尝试了这个查询较低的部分,但没有工作

tbl_nt_123.objects.values_list('id','address').exclude(condition_id=tbl_conditions 
   .objects.filter(version_id=5).values_list('condition_id',flat=True))

How can i do this?

我怎样才能做到这一点?

Please refer me some good links or books for understand advance django queries.

请参考我一些很好的链接或书籍,以了解提前django查询。

Thank you

1 个解决方案

#1


7  

Probably you should just add the __in lookup modifier:

可能你应该只添加__in查找修饰符:

tbl_nt_123.objects.values_list('id','address').exclude(
    condition_id__in=tbl_conditions.objects.filter(version_id=5).values_list('condition_id',flat=True))

As for the union, you can fake it using the | operator.

至于联盟,你可以使用|伪造它运营商。

union = queryset1 | queryset2

#1


7  

Probably you should just add the __in lookup modifier:

可能你应该只添加__in查找修饰符:

tbl_nt_123.objects.values_list('id','address').exclude(
    condition_id__in=tbl_conditions.objects.filter(version_id=5).values_list('condition_id',flat=True))

As for the union, you can fake it using the | operator.

至于联盟,你可以使用|伪造它运营商。

union = queryset1 | queryset2