在多个表中使用 min()
我的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此查询应该是您所需要的:
This query should be what you need:
我假设您需要所有表中的数据,这是一个
JOIN
后跟一个GROUP BY
以利用MIN()
函数:< 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 aGROUP BY
to utilize theMIN()
function:MySQL manual on GROUP BY-agregated functions
也许我错过了一些东西,但这看起来很简单......
Perhaps I'm missing something but this seems straightforward...