如何在连续预订之间找到第一个有2天免费插槽的房间

时间:2016-11-08 05:36:25

标签: mysql sql

我有这样的表:

<?php

namespace Wolnosciowiec\FileRepositoryBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\Serializer\Serializer;

class CacheCompilerPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        $config = $container->getExtensionConfig('file_repository');

        // inject the cache service into the Comrade Reader
        $comrade = $container->getDefinition('wolnosciowiec.comrade.reader');

        $comrade->addMethodCall('setUrl',        [$config[0]['url']]);
        $comrade->addMethodCall('setToken',      [$config[0]['token']]);

        // serializer
        $serializer = new Definition(Serializer::class, []);
        $serializer->setPublic(false);
        $container->setDefinition('wolnosciowiec.file_repository.serializer', $serializer);
        $comrade->addMethodCall('setSerializer', array($serializer));

        // cache
        $cache = new Definition($config[0]['cache_class'], []);
        $cache->setPublic(false);
        $container->setDefinition('wolnosciowiec.file_repository.cache', $cache);
        $comrade->addMethodCall('setCache', array($cache));
    }
}

我试着找到第一个房间,免费连续2天。

在这种情况下,答案是 102室,因为在2016年2月10日和5-10-2016预订,3-10和4-10是免费的。 你能帮帮我吗?

链接到SQL Fiddle

5 个答案:

答案 0 :(得分:1)

请试一试。

Select top 1 * from (
    Select a.room_id,DATEDIFF(DAY,a.date_reservartion,b.date_reservartion)-1 as Diff
    FROM room as a  
    INNER JOIN room as b on a.room_id=b.Room_id
) as t Where Diff = 2
Order by room_id

显示102见..

enter image description here

答案 1 :(得分:0)

试试这个

 Select top 1 a.room_id,DATEDIFF(DAY,a.date_reservartion,b.date_reservartion)-1 as Diff
         FROM room as a  
            INNER JOIN room as b on a.room_id=b.room_id Where Diff = 2 Order by a.room_id

答案 2 :(得分:0)

我正在分享我的两种方法。

方法#1:

margin:0 auto;

方法#2:

以下是使用SELECT a.room_id FROM booking a INNER JOIN booking b ON a.room_id = b.room_id AND a.date_reservartion < b.date_reservartion WHERE NOT EXISTS ( SELECT * FROM booking c WHERE c.room_id = a.room_id AND c.date_reservartion BETWEEN (a.date_reservartion + INTERVAL 1 DAY) AND (b.date_reservartion - INTERVAL 1 DAY) ) AND DATEDIFF(b.date_reservartion,a.date_reservartion) = 3 ORDER BY a.date_reservartion LIMIT 1 实现此目的的另一种方法。

MySQL user defined variables

SEE DEMO

注意:

如果您只需要第一个免费房间,请在上述查询中添加以下两行:

SELECT 
a.room_id
FROM
(
    SELECT 
    *,
    IF(@prevRoom = room_id, @rn := @rn + 1,
         IF(@prevRoom := room_id, @rn := 1, @rn := 1)
    ) AS rn
    FROM booking
    CROSS JOIN (SELECT @prevRoom := 0, @rn := 1) AS var 
    ORDER BY room_id, date_reservartion
)AS a
INNER JOIN 
(
    SELECT 
    *,
    IF(@prevRoom2 = room_id, @rn2 := @rn2 + 1,
         IF(@prevRoom2 := room_id, @rn2 := 1, @rn2 := 1)
    ) AS rn
    FROM booking
    CROSS JOIN (SELECT @prevRoom2 := 0, @rn2 := 1) AS var 
    ORDER BY room_id, date_reservartion
) AS b

ON a.room_id = b.room_id AND a.rn = b.rn+1
WHERE DATEDIFF(a.date_reservartion,b.date_reservartion) = 3

答案 3 :(得分:0)

每个房间在2016-10-05之后都是免费的,这是一个sql,显示房间2天免费。您可以轻松修改它以查看任何免费期间(+间隔N天)。如果您应用限制1 ,您将看到第一个免费房间。

page.resources[req.url] = {
        url: req.url, // was just "url" which was undefined
        redirected: null, 
        redirectedURL: null,
        HSTS: null,
        maxage: null,
        includeSubdomains: null
};

答案 4 :(得分:0)

请尝试这个

SELECT top 1 room_id
  FROM (SELECT a.*, (b.date_reservartion - a.date_reservartion - 1) diff
          FROM (SELECT ROW_NUMBER () OVER (ORDER BY room_id) rn, room_id,
                       date_reservartion
                  FROM booking) a inner join
               (SELECT ROW_NUMBER () OVER (ORDER BY room_id) rn, room_id,
                       date_reservartion
                  FROM booking) b
         On a.room_id = b.room_id AND a.rn = b.rn - 1) t
 WHERE diff = 2

显示102