sql - 在 Odoo 的 SQL 查询中使用 ID 列表

标签 sql python-3.x postgresql odoo

需要将 ID 列表传递给 where 子句中的 sql 查询。 product_id 是 many2many 字段。 我的代码是

query = """
                    SELECT pt.name as product,sl.price_unit as price,sum(sl.qty_invoiced) as quantity from sale_order_line sl
                    JOIN sale_order so ON so.id = sl.order_id
                    JOIN product_product pp ON pp.id = sl.product_id
                    JOIN product_template pt ON pt.id = pp.product_tmpl_id
                    WHERE so.date_order >='"""+str(obj.start_date)+"""' 
                    and so.date_order <= '"""+str(obj.end_date)+"""' 
                    and so.partner_id ="""+str(obj.customer_id.id)+""" 
                    and sl.invoice_status = 'invoiced'
        """ 
    if obj.product_id:
        query +=""" and sl.product_id in """+str(obj.product_id.ids)
    query += """GROUP BY product,price"""

语法错误 sl.product_id in [13017, 11253, 1395] near '['

最佳答案

得到解决方案, 将 id 列表转换为元组

if obj.product_id:
        query +=""" and sl.product_id in %s"""
    query += """GROUP BY product,price"""
    self.env.cr.execute(query, [tuple(obj.product_id.ids)])

关于sql - 在 Odoo 的 SQL 查询中使用 ID 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57107025/

相关文章:

SQL:计算自上次成功以来的天数

SQl 将多个 CTE 的计数放入一个表中

mysql - GROUP BY a, b VS GROUP BY b, a

linux - 使用Python通过Serial读取不固定数量的字节

python - 奇怪的错误 - python manage.py runserver(线程 django-main-thread 中的异常)

Python 3 : find item in list and answer based on that

ruby-on-rails - Rails has_many :through PG::Error: ERROR: column reference "id"is ambiguous 错误

sql - 高效的sql素数算法

sql - 如何使用sql在数据 block 上创建带有嵌套 map 的表

mysql - 为什么查询 information_schema 需要时间?