使用来自多个sql数据集的多个左连接

u0sqgete  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(313)

我需要执行多个连接,我从多个表中获取数据并在id上连接。棘手的是一个表需要连接两次。代码如下:

(
    SELECT
        content.brand_identifier AS brand_name,
        CAST(timestamp(furniture.date) AS DATE) AS order_date,
total_hearst_commission
    FROM
        `furniture_table` AS furniture
        LEFT JOIN `content_table` AS content ON furniture.site_content_id = content.site_content_id
    WHERE
        (
            timestamp(furniture.date) >= TIMESTAMP('2020-06-01 00:00:00')
        )
)
UNION
(
    SELECT
        flowers.a_merchant_name AS merchant_name
    FROM
        `flowers_table` AS flowers
        LEFT JOIN `content` AS content ON flowers.site_content_id = content.site_content_id
)
GROUP BY
    1,
    2,
    3,
    4
ORDER BY
    4 DESC
LIMIT
    500

我以为我可以用union,但它给了我一个错误 Syntax error: Expected keyword ALL or keyword DISTINCT but got "("

yjghlzjz

yjghlzjz1#

我不能发表评论,但像ghb州一样,查询的列数不同;因此,工会不会在这里工作。
我认为首先知道为什么需要子查询会很有帮助。我猜这个查询不会产生您想要的结果,所以请详细说明原因。

select 
        f.a_merchant_name as merchant_name,
        c.brand_identifier as brand_name, 
        CAST(timestamp(f.date) AS DATE) AS order_date,
        total_hearst_commission
    from furniture_table f
    left join content_table c on c.site_content_id = f.site_content_id
    where timestamp(f.date) >= TIMESTAMP('2020-06-01 00:00:00')
    group by 1,2,3,4

相关问题