MySQL 更新内连接别名
我正在尝试运行此查询:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用 Min 而不是 Least:
编辑
正如注释中所指出的,我在内部临时查询中省略了
to
列。然而,我发现不清楚如何包含to
,因为您在声明 Group By 列方面使用了 MySQL 的一个糟糕功能。我假设您需要在 Group By 中包含to
,但是如果情况并非如此,您应该明确说明它应该在to
上使用什么聚合函数柱子。这是我在
to
列上使用 Min 的替代方案:You should use Min instead of Least:
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 howto
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 includeto
in the Group By however if that is not the case, you should be explicit about what aggregate function it should use on theto
column.Here's an alternate where I use Min on the
to
column: