MySQL 更新内连接别名

发布于 2024-10-19 14:56:09 字数 739 浏览 1 评论 0原文

我正在尝试运行此查询:

UPDATE anothertable
INNER JOIN (SELECT *, 
                   LEAST(table1.from_price, table2.from_price, table3.from_price) AS cheapestPrice
              FROM (SELECT * FROM table1 v WHERE hotelid >= 1
                    UNION
                    SELECT * FROM table2 c WHERE hotelid >= 1
                    UNION
                    SELECT * FROM table3 k WHERE hotelid >= 1) AS temp      
           GROUP BY temp.hotelid, temp.country) AS i ON anothertable.id = i.hotelid 
                                                    AND anothertable.country = i.country
SET price = i.cheapestPrice, 
    op = i.to

但是我无法使用 LEAST 函数来访问名为“from_price”的字段。

有想法吗?

I'm trying to run this query:

UPDATE anothertable
INNER JOIN (SELECT *, 
                   LEAST(table1.from_price, table2.from_price, table3.from_price) AS cheapestPrice
              FROM (SELECT * FROM table1 v WHERE hotelid >= 1
                    UNION
                    SELECT * FROM table2 c WHERE hotelid >= 1
                    UNION
                    SELECT * FROM table3 k WHERE hotelid >= 1) AS temp      
           GROUP BY temp.hotelid, temp.country) AS i ON anothertable.id = i.hotelid 
                                                    AND anothertable.country = i.country
SET price = i.cheapestPrice, 
    op = i.to

However I cannot get the LEAST function to get access to a field called "from_price".

Ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江南月 2024-10-26 14:56:09

您应该使用 Min 而不是 Least:

Update anothertable
    Join    (
            Select hotelid, country, to
                , Min(from_price) AS cheapestPrice
            From    (
                    Select hotelid, country, from_price, to
                    From table1 v 
                    Where hotelid >= 1
                    Union
                    Select hotelid, country, from_price, to
                    From table2 c 
                    Where hotelid >= 1
                    Union
                    Select hotelid, country, from_price, to
                    From table3 k 
                    Where hotelid >= 1
                    ) AS temp
            Group By temp.hotelid, temp.country, temp.to
            ) As i 
        On anothertable.id = i.hotelid 
            And anothertable.country = i.country
Set price = i.cheapestPrice
    , op = i.to

编辑

正如注释中所指出的,我在内部临时查询中省略了 to 列。然而,我发现不清楚如何包含 to ,因为您在声明 Group By 列方面使用了 MySQL 的一个糟糕功能。我假设您需要在 Group By 中包含 to ,但是如果情况并非如此,您应该明确说明它应该在 to 上使用什么聚合函数柱子。

这是我在 to 列上使用 Min 的替代方案:

Update anothertable
    Join    (
            Select temp.hotelid, temp.country
                , Min(temp.to) As to
                , Min(temp.from_price) AS cheapestPrice
            From    (
                    Select v.hotelid, v.country, v.from_price, v.to
                    From table1 v 
                    Where hotelid >= 1
                    Union
                    Select c.hotelid, c.country, c.from_price, c.to
                    From table2 c 
                    Where hotelid >= 1
                    Union
                    Select k.hotelid, k.country, k.from_price, k.to
                    From table3 k 
                    Where hotelid >= 1
                    ) AS temp
            Group By temp.hotelid, temp.country
            ) As i 
        On anothertable.id = i.hotelid 
            And anothertable.country = i.country
Set price = i.cheapestPrice
    , op = i.to

You should use Min instead of Least:

Update anothertable
    Join    (
            Select hotelid, country, to
                , Min(from_price) AS cheapestPrice
            From    (
                    Select hotelid, country, from_price, to
                    From table1 v 
                    Where hotelid >= 1
                    Union
                    Select hotelid, country, from_price, to
                    From table2 c 
                    Where hotelid >= 1
                    Union
                    Select hotelid, country, from_price, to
                    From table3 k 
                    Where hotelid >= 1
                    ) AS temp
            Group By temp.hotelid, temp.country, temp.to
            ) As i 
        On anothertable.id = i.hotelid 
            And anothertable.country = i.country
Set price = i.cheapestPrice
    , op = i.to

Edit

As pointed out in comments, I omitted the to column from the inner temp query. However, it occurs to me that it isn't clear how to should be included because you are using an awful feature of MySQL with respect to declaring the Group By columns. I'm assuming that you need to include to in the Group By however if that is not the case, you should be explicit about what aggregate function it should use on the to column.

Here's an alternate where I use Min on the to column:

Update anothertable
    Join    (
            Select temp.hotelid, temp.country
                , Min(temp.to) As to
                , Min(temp.from_price) AS cheapestPrice
            From    (
                    Select v.hotelid, v.country, v.from_price, v.to
                    From table1 v 
                    Where hotelid >= 1
                    Union
                    Select c.hotelid, c.country, c.from_price, c.to
                    From table2 c 
                    Where hotelid >= 1
                    Union
                    Select k.hotelid, k.country, k.from_price, k.to
                    From table3 k 
                    Where hotelid >= 1
                    ) AS temp
            Group By temp.hotelid, temp.country
            ) As i 
        On anothertable.id = i.hotelid 
            And anothertable.country = i.country
Set price = i.cheapestPrice
    , op = i.to
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文