相关疑难解决方法(0)

sql查询中的python列表作为参数

我有一个python列表,比如l

l = [1,5,8]
Run Code Online (Sandbox Code Playgroud)

我想编写一个sql查询来获取列表中所有元素的数据

select name from students where id = |IN THE LIST l|
Run Code Online (Sandbox Code Playgroud)

我该如何做到这一点?

python sql

106
推荐指数
8
解决办法
9万
查看次数

Django使用元组列表过滤多个列

我有一个简单的模型有2个字段:

class Simple(Model)
    class Meta:
        index_together = True

    a = IntField()
    b = IntField()
Run Code Online (Sandbox Code Playgroud)

我想为值的元组生成一个SQL查询a,b.例如

select * 
from SimpleModel 
where (a,b) in ((1,1), (4,8), ...)
Run Code Online (Sandbox Code Playgroud)

我知道如何创建类似的东西:

select * 
from SimpleModel 
where ((a = 1 and b = 1) or (a = 4 and b = 8))
Run Code Online (Sandbox Code Playgroud)

这在逻辑上是相同的,但我认为我的数据库在可能值的数量非常大(我使用Postgresql)时会出现问题,查询本身也会长得多,所以它在网络上更重,并且可能更难正确分析和读取(即在这种情况下使用复合索引).

所以,问题是,我可以让Django以第一种形式创建查询吗?

谢谢!

sql django-models

4
推荐指数
2
解决办法
1807
查看次数

标签 统计

sql ×2

django-models ×1

python ×1