SQL - 嵌套子查询

SQL - Nested Sub-Queries

使用 postgresql,我必须创建一个名为 'no_cat_customers' 的视图,其中 returns 任何尚未发货的任何客户的姓氏和名字 'The Cat in the Hat'.这应该使用通用量化来完成。我得到的错误是 - "ERROR: EXCEPT types integer and text cannot be matched" 参考第 7 行 (EXCEPT (SELECT shipments.isbn).

CREATE VIEW no_cat_customers(customer_first_name, customer_last_name)
AS SELECT first_name, last_name
FROM customers
WHERE EXISTS (SELECT c_id
              FROM shipments
              WHERE customers.c_id = shipments.c_id 
              EXCEPT (SELECT shipments.isbn
                      FROM books, editions, shipments
                      WHERE books.book_id = editions.book_id AND
                        editions.isbn = shipments.isbn AND
                        title LIKE '%The Cat in the Hat%'));

我知道考虑到您没有我正在使用的数据库,这很难问,但是非常感谢任何对此的帮助!

编辑:我应该补充一点,数据库中有两个版本的 'The Cat in the Hat',它们都有不同的 isbn。所以必须考虑到这一点。

使用明确的 JOIN 语法,而不是在 where 子句中将其与实际条件混合。我相信这应该有效:

CREATE VIEW no_cat_customers(customer_first_name, customer_last_name) AS 
SELECT first_name, last_name
FROM customers c
WHERE NOT EXISTS (
  SELECT 1 
  FROM shipments s
  JOIN editions e ON s.isbn = e.isbn
  JOIN books b ON b.book_id = e.book_id
  WHERE b.title ILIKE '%The Cat in the Hat%'
  AND c.c_id = s.c_id
  )

如果您有数据类型错误,请在比较之前将您的列转换为适当的类型。