查询不在列表中的项目,在一列中没有唯一 id

Query items that are not in the list, without having a unique id in one column

我想创建一个 Python 脚本来显示商店(位置 1)中不再可用但在仓库(位置 99)中可用的项目。例如。需要补货的物品清单。困难在于,库存管理系统没有将商店的商品计数置为 0,而是将商品从数据库中完全删除。此外,商品没有唯一 ID,而是序列号和商品尺寸两列的组合。

(PostgreSQL) 数据库 restock 其中一个 table inventory 看起来像这样:

====================================================================
| serialnumber  | size     | location     | itemcount   | season   |
--------------------------------------------------------------------
| 1120.10.0369  | 140      | 99           | 8           | 74       |
| 1120.10.0369  | 140      | 1            | 2           | 74       |
| 1120.10.4011  | 170/176  | 99           | 3           | 74       |
| 1120.10.4011  | 170/176  | 1            | 2           | 74       |
| 1120.10.4011  | 86/92    | 99           | 1           | 74       |
| 1120.10.8006  | 158      | 99           | 1           | 74       |
| 1120.10.8006  | 158      | 1            | 2           | 74       |

在上面的示例中,序列号为 1120.10.4011 且尺寸为 86/92 的项目在位置 99(仓库)可用,但没有序列号为 [= 的行14=] 和尺码 86/92,位置是 1,所以我想在清单上重新进货的商品。

我试图通过在 GROUP BY 查询上使用计数来显示数据中不存在的项目:

getresult = "SELECT serialnumber, size, location, itemcount, season, COUNT(*)" + \
            "FROM inventory " + \
            "WHERE season = 74" + \
            "AND location != 1" + \
            "GROUP BY serialnumber, size " + \
            "HAVING COUNT(*) < 1"

但是,这并不像预期的那样有效。

问题依旧:

getresult = """
select i99.serialnumber, i99.size, i99.itemcount, i99.season
from 
    inventory i99
    left join
    inventory i1 on
        (i99.serialnumber, i99.size, i99.season, i1.season, i99.location, i1.location) =
        (i1.serianumber, i1.size, 74, 74, 99, 1)
where i1.size is null
"""

您必须在不存在的子句中使用子select:

getresult = """SELECT serialnumber, size FROM inventory i99
    WHERE season = 74 AND location = 99
    AND NOT EXISTS (SELECT serialnumber FROM inventory i1
        WHERE i1.serialnumber = i99.serialnumber
        AND i1.size = i99.size
        AND i1.location = 1)"""

这是我知道直接在 SQL 级别找到不存在值的唯一方法,因为 IS NULL 即使在外部连接测试中,该值也是原始值 table 并且不是 select.

返回的内容