【问题标题】:Nested SQL query?嵌套 SQL 查询?
【发布时间】:2019-07-19 02:22:02
【问题描述】:

我有两张这样的桌子:

product_table[ product_id, product_name,original_store_id, destination_store id]

和

store_table[ store_id, establishment_date, location]

我要查找的是:从所有那些发货到没有成立日期的商店的产品中,有多少是从有成立日期的商店发货的?

这是我的查询:

SELECT count(a.product_id) as count_of_products
FROM product_table a
JOIN store_table b
ON a.original_store_id = b.store_id AND a.destination_store_id = b.store_id
WHERE b.establishment_date IS NULL

我知道这应该是一个嵌套查询,但是如何将它们放在这里?

【问题讨论】:

    标签: mysql sql google-bigquery


    【解决方案1】:

    您可以尝试在store_table 表中添加第二个连接,以进一步限制仅从具有成立日期的商店发货的产品:

    SELECT COUNT(a.product_id) AS count_of_products
    FROM product_table a
    INNER JOIN store_table b
        ON a.destination_store_id = b.store_id
    INNER JOIN store_table c
        ON a.original_store_id = c.store_id
    WHERE
        b.establishment_date IS NULL AND
        c.establishment_date IS NOT NULL;
    

    【讨论】:

    • 。 .使用有意义的表别名,而不是无意义的字母。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-24
    相关资源
    最近更新 更多