在多个表中使用 min()

发布于 2024-12-15 13:29:21 字数 1230 浏览 0 评论 0原文

我的sql知识不太好,经过2个小时的头脑风暴,我决定向这里的朋友们请教。

这是我的问题,我正在开发一个旅游网站。有地区和价格表。结构基本上是这样的。

regions             region_contents         prices
-------             ----------------        --------
regionId            regionId                priceId
regionName          hotelId                 hotelId
someMoreFields      someMoreFields          price
                                            beginDate
                                            endDate
                                            someMoreFields

我想要从这些数据中得到的是

  +-----------------+-------------+-------------------------+
  |        hotelId  |  hotelName  |  min price for this Id  |
  +-----------------+-------------+-------------------------+
  |        hotelId  |  hotelName  |  min price for this Id  |
  +-----------------+-------------+-------------------------+
  |        hotelId  |  hotelName  |  min price for this Id  |
  +-----------------+-------------+-------------------------+
  |        hotelId  |  hotelName  |  min price for this Id  |
  +-----------------+-------------+-------------------------+

我查询特定区域中的所有酒店,按每家酒店的最低价格排序,并且每家酒店的价格结束日期应该晚于 NOW()。

提前致谢。 还对语法错误表示歉意。

My sql knowledge is not that good and after 2 hours of brainstorm i decided to ask my fellow friends in here.

Here is my problem, i am developing a travel website. There are regions and prices table. structure is basicly like this.

regions             region_contents         prices
-------             ----------------        --------
regionId            regionId                priceId
regionName          hotelId                 hotelId
someMoreFields      someMoreFields          price
                                            beginDate
                                            endDate
                                            someMoreFields

What i want out of this data is

  +-----------------+-------------+-------------------------+
  |        hotelId  |  hotelName  |  min price for this Id  |
  +-----------------+-------------+-------------------------+
  |        hotelId  |  hotelName  |  min price for this Id  |
  +-----------------+-------------+-------------------------+
  |        hotelId  |  hotelName  |  min price for this Id  |
  +-----------------+-------------+-------------------------+
  |        hotelId  |  hotelName  |  min price for this Id  |
  +-----------------+-------------+-------------------------+

I query for all the hotels in spesific region ordered by min price for each hotel and the endDate of price for each hotel should be later than NOW().

Thanks in advance.
Also apoligies for bad grammar.

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

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

发布评论

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

评论(3

我早已燃尽 2024-12-22 13:29:21

此查询应该是您所需要的:

SELECT hotel_id, hotel_name, MIN(price)
FROM region
JOIN region_contents 
   ON regionName = <reqdname> AND region.regionID = region_contents.regionID
JOIN prices 
   ON region_contents.hotel_id = prices_hotel_id
WHERE end_date > NOW()
GROUP BY hotel_id
ORDER BY MIN(price)

This query should be what you need:

SELECT hotel_id, hotel_name, MIN(price)
FROM region
JOIN region_contents 
   ON regionName = <reqdname> AND region.regionID = region_contents.regionID
JOIN prices 
   ON region_contents.hotel_id = prices_hotel_id
WHERE end_date > NOW()
GROUP BY hotel_id
ORDER BY MIN(price)
情独悲 2024-12-22 13:29:21

我假设您需要所有表中的数据,这是一个 JOIN 后跟一个 GROUP BY 以利用 MIN() 函数:

SELECT rc.hotelId, rc.hotelName, MIN(p.price) FROM regions r
LEFT JOIN region_contents rc ON r.regionId = rc.regionId
LEFT JOIN prices p ON p.hotelId = rc.hotelID
GROUP BY rc.hotelId

< a href="http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_min" rel="nofollow">有关 GROUP BY 聚合函数的 MySQL 手册

I asume that you need data from all your tables, which is a JOIN followed by a GROUP BY to utilize the MIN() function:

SELECT rc.hotelId, rc.hotelName, MIN(p.price) FROM regions r
LEFT JOIN region_contents rc ON r.regionId = rc.regionId
LEFT JOIN prices p ON p.hotelId = rc.hotelID
GROUP BY rc.hotelId

MySQL manual on GROUP BY-agregated functions

随遇而安 2024-12-22 13:29:21

也许我错过了一些东西,但这看起来很简单......

SELECT
 hotelId,
 hotelName,
 MIN(price)
FROM
 region_contents
INNER JOIN
 prices
ON
 region_contents.hotel_id = prices.hotel_id
GROUP BY
 hotelId,
 hotelName
ORDER BY
 MIN(price)

Perhaps I'm missing something but this seems straightforward...

SELECT
 hotelId,
 hotelName,
 MIN(price)
FROM
 region_contents
INNER JOIN
 prices
ON
 region_contents.hotel_id = prices.hotel_id
GROUP BY
 hotelId,
 hotelName
ORDER BY
 MIN(price)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文