我正在尝试在Athena中进行左ourter加入,我的查询如下所示:
SELECT customer.name, orders.price
FROM customer LEFT OUTER JOIN order
ON customer.id = orders.customer_id
WHERE price IS NULL;
每个客户最多只能在订单表中订购一个订单,而订单表中根本没有订单的客户。所以我希望获得一些记录,其中客户表中有一个客户,订单表中没有记录,这意味着当我LEFT OUTER JOIN
时,价格将为NULL。但是每次运行它时,此查询都返回0。我分别对这两个表进行查询,并且非常确定两者中都有数据,但不确定为什么如果我删除price IS NULL
,它会返回零。我也尝试了price = ''
和price IN ('')
,但没有一个有效。以前有没有人有类似的经历?或者我的查询有什么问题我无法查看或识别?
答案 0 :(得分:0)
您的查询似乎正确。为了进行验证,我创建了两个CTE,它们应该与您的customer
和orders
表匹配,并针对它们运行查询。当运行下面的查询时,它为没有订单的客户3 Ted Johnson
返回一条记录。
WITH customer AS (
SELECT 1 AS id, 'John Doe' AS name
UNION
SELECT 2 AS id, 'Jane Smith' AS name
UNION
SELECT 3 AS id, 'Ted Johnson' AS name
),
orders AS (
SELECT 1 AS customer_id, 20 AS price
UNION
SELECT 2 AS customer_id, 15 AS price
)
SELECT customer.name, orders.price
FROM customer LEFT OUTER JOIN orders
ON customer.id = orders.customer_id
WHERE price IS NULL;
我建议运行以下查询:
COUNT(DISTINCT id) FROM customers;
COUNT(DISTINCT customer_id) FROM orders;
根据您看到的结果,我希望这些计数能够匹配。每当创建orders
为0的客户时,系统可能会在price
表中创建一条记录。
答案 1 :(得分:0)
可能where
不能使用order table
。
SELECT customer.name, order.price
FROM customer LEFT OUTER JOIN order
ON customer.id = orders.customer_id AND order.price IS NULL;