如果这个id不存在于另一个表中,如何过滤?

xxb16uws  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(515)

我试着用这个代码过滤b列中是否不存在a列的id。

query = db.session.query().select_from(Spare_Parts, Vendors, Replacement)\
            .filter(Vendors.vendor_code == Spare_Parts.vendor_code,\
            ~ exists().where(Spare_Parts.spare_part_code == Replacement.spare_part_code))

我想从中查询数据 Spare_Parts 中不存在id的 Replacement 但我犯了这样的错误。

Select statement 'SELECT * 
FROM spare_parts, replacement 
WHERE spare_parts.spare_part_code = replacement.spare_part_code' returned no FROM clauses due to auto-correlation; specify correlate(<tables>) to control correlation manually.

那么什么是问题以及如何解决。

i5desfxk

i5desfxk1#

尝试像这样使用子查询
从下列备件中筛选备件代码: not in 替换表``

SELECT * 
FROM spare_parts
WHERE spare_parts.spare_part_code not in 
    (select distinct 
    replacement.spare_part_code
    FROM replacement)

或者你可以用 not exists ```
SELECT *
FROM spare_parts
WHERE not exists
(select 1
FROM replacement
where spare_parts.spare_parts_code = replacement.spare_parts_code)

相关问题