SQL 自然连接 POSTGRES
我不确定我需要什么样的连接,因为我不熟悉尝试以这种方式重叠数据,或者它是否可行。
我有两个表,它们共享一组相似的数据,并且都通过 Room_id 与第三个父表相关。
我有一个名为 Room_rates 的表,它存储每个房间的平均价格 (room_id)
+-------+---------+-----------+-------+--------+---------------------------+---------------------------+
| id | room_id | dayofweek | price | source | created_at | updated_at |
+-------+---------+-----------+-------+--------+---------------------------+---------------------------+
| 87936 | 2517 | 0 | 14.58 | 1 | 2010-02-22 17:47:14 +0100 | 2010-02-22 17:47:14 +0100 |
| 87937 | 2517 | 1 | 14.58 | 1 | 2010-02-22 17:47:14 +0100 | 2010-02-22 17:47:14 +0100 |
| 87938 | 2517 | 2 | 14.52 | 1 | 2010-02-22 17:47:14 +0100 | 2010-02-22 17:47:14 +0100 |
| 87939 | 2517 | 3 | 14.52 | 1 | 2010-02-22 17:47:14 +0100 | 2010-02-22 17:47:14 +0100 |
| 87940 | 2517 | 4 | 14.52 | 1 | 2010-02-22 17:47:15 +0100 | 2010-02-22 17:47:15 +0100 |
| 87941 | 2517 | 5 | 14.4 | 1 | 2010-02-22 17:47:15 +0100 | 2010-02-22 17:47:15 +0100 |
| 87942 | 2517 | 6 | 14.63 | 1 | 2010-02-22 17:47:15 +0100 | 2010-02-22 17:47:15 +0100 |
+-------+---------+-----------+-------+--------+---------------------------+---------------------------+
和一个表称为“Availables”,具有特定日期的费率
+--------+-------+-------+------------+---------+---------------------------+---------------------------+--------+
| id | price | spots | bookdate | room_id | created_at | updated_at | source |
+--------+-------+-------+------------+---------+---------------------------+---------------------------+--------+
| 221389 | 14.3 | 1 | 2010-03-01 | 2517 | 2010-02-21 22:31:06 +0100 | 2010-02-21 22:31:06 +0100 | 1 |
| 221390 | 14.3 | 1 | 2010-03-02 | 2517 | 2010-02-21 22:31:06 +0100 | 2010-02-21 22:31:06 +0100 | 1 |
| 221391 | 14.3 | 1 | 2010-03-03 | 2517 | 2010-02-21 22:31:06 +0100 | 2010-02-21 22:31:06 +0100 | 1 |
| 221392 | 14.3 | 1 | 2010-03-04 | 2517 | 2010-02-21 22:31:06 +0100 | 2010-02-22 17:47:19 +0100 | 1 |
| 221393 | | 0 | 2010-03-05 | 2517 | 2010-02-21 22:31:06 +0100 | 2010-02-22 17:47:19 +0100 | 1 |
| 221394 | | 0 | 2010-03-06 | 2517 | 2010-02-21 22:31:06 +0100 | 2010-02-22 17:47:19 +0100 | 1 |
| 228185 | | 0 | 2010-03-07 | 2517 | 2010-02-22 17:47:19 +0100 | 2010-02-22 17:47:19 +0100 | 1 |
| 228186 | 14.3 | 1 | 2010-03-08 | 2517 | 2010-02-22 17:47:19 +0100 | 2010-02-22 17:47:19 +0100 | 1 |
| 228187 | 14.3 | 1 | 2010-03-09 | 2517 | 2010-02-22 17:47:19 +0100 | 2010-02-22 17:47:19 +0100 | 1 |
| 228188 | 14.3 | 1 | 2010-03-10 | 2517 | 2010-02-22 17:47:19 +0100 | 2010-02-22 17:47:19 +0100 | 1 |
+--------+-------+-------+------------+---------+---------------------------+---------------------------+--------+
目前我正在使用两个单独的搜索结果,特定日期范围的当前 Availables
数据要么存在,要么不存在。如果没有,我将使用仅使用 Room_rate
平均值的备份查询。
我想在有可用价格的地方使用“Availables”,但如果价格不可用或可能根本没有记录,则以某种方式加入 Room_rates 来填补空白。
我怎样才能做到这一点?
I'm not sure what kind of join I need as I'm not familiar with trying to overlap data in such a way or if it's even feasible.
I have two tables which both share a similar set of data and are both related to a 3rd parent table thru Room_id.
I have a table called Room_rates which stores average prices for each room (room_id)
+-------+---------+-----------+-------+--------+---------------------------+---------------------------+
| id | room_id | dayofweek | price | source | created_at | updated_at |
+-------+---------+-----------+-------+--------+---------------------------+---------------------------+
| 87936 | 2517 | 0 | 14.58 | 1 | 2010-02-22 17:47:14 +0100 | 2010-02-22 17:47:14 +0100 |
| 87937 | 2517 | 1 | 14.58 | 1 | 2010-02-22 17:47:14 +0100 | 2010-02-22 17:47:14 +0100 |
| 87938 | 2517 | 2 | 14.52 | 1 | 2010-02-22 17:47:14 +0100 | 2010-02-22 17:47:14 +0100 |
| 87939 | 2517 | 3 | 14.52 | 1 | 2010-02-22 17:47:14 +0100 | 2010-02-22 17:47:14 +0100 |
| 87940 | 2517 | 4 | 14.52 | 1 | 2010-02-22 17:47:15 +0100 | 2010-02-22 17:47:15 +0100 |
| 87941 | 2517 | 5 | 14.4 | 1 | 2010-02-22 17:47:15 +0100 | 2010-02-22 17:47:15 +0100 |
| 87942 | 2517 | 6 | 14.63 | 1 | 2010-02-22 17:47:15 +0100 | 2010-02-22 17:47:15 +0100 |
+-------+---------+-----------+-------+--------+---------------------------+---------------------------+
And a table called Availables which has rates for specific dates
+--------+-------+-------+------------+---------+---------------------------+---------------------------+--------+
| id | price | spots | bookdate | room_id | created_at | updated_at | source |
+--------+-------+-------+------------+---------+---------------------------+---------------------------+--------+
| 221389 | 14.3 | 1 | 2010-03-01 | 2517 | 2010-02-21 22:31:06 +0100 | 2010-02-21 22:31:06 +0100 | 1 |
| 221390 | 14.3 | 1 | 2010-03-02 | 2517 | 2010-02-21 22:31:06 +0100 | 2010-02-21 22:31:06 +0100 | 1 |
| 221391 | 14.3 | 1 | 2010-03-03 | 2517 | 2010-02-21 22:31:06 +0100 | 2010-02-21 22:31:06 +0100 | 1 |
| 221392 | 14.3 | 1 | 2010-03-04 | 2517 | 2010-02-21 22:31:06 +0100 | 2010-02-22 17:47:19 +0100 | 1 |
| 221393 | | 0 | 2010-03-05 | 2517 | 2010-02-21 22:31:06 +0100 | 2010-02-22 17:47:19 +0100 | 1 |
| 221394 | | 0 | 2010-03-06 | 2517 | 2010-02-21 22:31:06 +0100 | 2010-02-22 17:47:19 +0100 | 1 |
| 228185 | | 0 | 2010-03-07 | 2517 | 2010-02-22 17:47:19 +0100 | 2010-02-22 17:47:19 +0100 | 1 |
| 228186 | 14.3 | 1 | 2010-03-08 | 2517 | 2010-02-22 17:47:19 +0100 | 2010-02-22 17:47:19 +0100 | 1 |
| 228187 | 14.3 | 1 | 2010-03-09 | 2517 | 2010-02-22 17:47:19 +0100 | 2010-02-22 17:47:19 +0100 | 1 |
| 228188 | 14.3 | 1 | 2010-03-10 | 2517 | 2010-02-22 17:47:19 +0100 | 2010-02-22 17:47:19 +0100 | 1 |
+--------+-------+-------+------------+---------+---------------------------+---------------------------+--------+
Currently I am using two separate search results, either current Availables
data for a specific date range exists or it doesn't. If it doesn't I use a back up query using only Room_rate
averages.
I would like to use Availables where it has available prices, but somehow join Room_rates to fill in the blanks if the price is unavailable or possibly there is no record at all.
How could I accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
COALESCE
函数可以与联接相结合来满足您的需求:您需要在适当的列上建立索引才能提高性能。您还需要比较执行计划针对替代方案(例如 Bruno 建议的
UNION
)的声明,看看连接是否值得。The
COALESCE
function could be combined with a join to give you what you want:You'll need indices on the appropriate columns for this to be performant. You'll also need to compare the execution plan of this statement against alternatives (such as the
UNION
suggested by Bruno) to see if the join is worth it.连接的情况并非如此。连接会将一个表中的数据与另一个表中的数据进行匹配。例如,为了匹配 room 和 room_rates,您将使用联接。但您不想匹配“Available”和“Room_rates”。您想要从“可用”中获取房价,或者(如果在“可用”中找不到)从 Room_rates 中获取房价。
因此,您确实必须首先对“Available”运行查询,然后在需要时对“Room_rates”运行查询。
您可以获得查询返回的第一个结果,如下所示:
但我真的不建议这样做,因为即使不需要,也会运行第二个“选择”。因此,最好仅在需要时运行第一个选择,然后运行第二个选择。
This is not the case for a join. A join will match data from a table to data from another table. For example, for matching room and room_rates you would use a join. But you don't want to match Available and Room_rates. You want to get the rate from Available or - if not found in Avaiable- from Room_rates.
So, you really have to first run a query on Available and then a query on Room_rates if needed.
You could get the first result returned by a query that is something like:
But I would not really recommend that because the second "select" would be run even if not needed. So it's better to run the first select and then the second select only if needed.