从同一个表mysql中获取记录

时间:2017-04-28 19:37:36

标签: mysql

提前致谢。

我必须验证某个用户在某个时间是否可用。我试图通过将原始记录(具有我们将要与活动记录的日期和时间进行比较的值)与表的其余部分进行比较,对同一个表进行双重查询,所以想要分配时间表的用户必须验证它不是在同一时间或在一段时间内(例如前后45分钟)在其他服务中分配的。

我无法解决问题,我想知道你是否可以帮我解决这个问题。这是我到目前为止的查询,但它不会返回任何结果:

SELECT b2.*, b.* FROM bookingdetails b, bookingdetails b2
WHERE b.pickup_date = b2.pickup_date
AND b.pickup_time = b2.pickup_time
AND b2.driver_id = 109
AND b.id = 2449
AND b2.id <> 2449
AND b2.pickup_time BETWEEN b.pickup_time - INTERVAL 45 MINUTE AND b.pickup_time + INTERVAL 45 MINUTE

1 个答案:

答案 0 :(得分:1)

我认为你走在正确的轨道上,但你有两个竞争条件:

b.pickup_time = b2.pickup_time

b2.pickup_time BETWEEN b.pickup_time - INTERVAL 45 MINUTE AND b.pickup_time + INTERVAL 45 MINUTE

摆脱第一个,我认为它可能适合你。

此外,值得注意的是您使用的是过时的JOIN语法。代替:

SELECT b2.*,
    b.*
FROM bookingdetails b
    INNER JOIN bookingdetails b2 ON 
        b.pickup_date = b2.pickup_date      
        AND b2.pickup_time BETWEEN b.pickup_time - INTERVAL 45 MINUTE
            AND b.pickup_time + INTERVAL 45 MINUTE
WHERE b2.driver_id = 109
    AND b.id = 2449
    AND b2.id <> 2449