MySQL:更改每个选定行的用户变量

MySQL:更改每个选定行的用户变量,mysql,sql,user-variables,Mysql,Sql,User Variables,我试图在MySQL数据库中选择约会之间的前十个空时段 约会表基本上有3个字段:约会id INT、startDateTime DATETIME和endDateTime DATETIME 为了简单起见,我们可以想象一些这样的数据,我把日期部分离开了日期时间,所以让我们考虑一下这些时间是在同一天。此外,数据按startDateTime排序: 4 | 09:15:00 | 09:30:00 5 | 09:30:00 | 09:45:00 8 | 10:00:00 | 10:15:00 3 | 10:30

我试图在MySQL数据库中选择约会之间的前十个空时段

约会表基本上有3个字段:约会id INT、startDateTime DATETIME和endDateTime DATETIME

为了简单起见,我们可以想象一些这样的数据,我把日期部分离开了日期时间,所以让我们考虑一下这些时间是在同一天。此外,数据按startDateTime排序:

4 | 09:15:00 | 09:30:00
5 | 09:30:00 | 09:45:00
8 | 10:00:00 | 10:15:00
3 | 10:30:00 | 10:45:00
7 | 10:45:00 | 11:00:00
2 | 11:00:00 | 11:15:00
1 | 11:30:00 | 12:00:00
因此,我的目标是提取:

00:00:00 | 09:15:00
09:45:00 | 10:00:00
10:15:00 | 10:30:00
11:15:00 | 11:30:00
最后,我做了这样一件事:

SET @myStart = '2012-10-01 09:15:00';
SET @myEnd = NULL;
SET @prevEnd = NULL;
SELECT a.endDateTime, b.startDateTime, @myStart := a.endDateTime
FROM appointment a, appointment b, (
    SELECT @myEnd := min(c.startDateTime) 
    FROM appointment c 
    WHERE c.startDateTime >= @myStart
    ORDER BY startDateTime ASC
) as var , 
(SELECT @prevEnd := NULL) v
WHERE a.appointment_id = (
    SELECT appointment_id 
    FROM (
        SELECT appointment_id, max(endDateTime), @prevEnd := endDateTime 
        FROM appointment d
        WHERE (@prevEnd IS NULL OR @prevEnd = d.startDateTime) 
            AND d.startDateTime >= @myEnd
    ) as z
) 
    AND b.startDateTime > a.endDateTime 
ORDER BY b.startDateTime ASC LIMIT 0,10;
这不会返回任何结果。我想这是因为我的用户定义变量的初始化不正确,我刚刚发现了它们,我可能完全错误地使用了它们

如果我只运行第一个子查询,其目标是在@myStart之后的第一次约会中初始化@myEnd,我可以看到它实际上返回09:15:00

第二个子查询SELECT@prevEnd:=NULL v意味着每次在主查询中选择一行时将@prevEnd设置回NULL。我不太确定它是这样工作的

最后一个子查询的意思是,从null@prevEnd和初始化的@myEnd开始,选择有间隔的约会。如果与查询的其余部分分开,我可以验证它是否也可以工作

你对我能做些什么来解决这个问题,我能/应该怎么做,或者它是否可能,有什么建议吗

非常感谢

编辑:我对其进行了如下编辑:

SELECT * 
    FROM (
        SELECT COALESCE( s1.endDateTime,  '0000-00-00 00:00:00' ) AS myStart, MIN( s2.startDateTime ) AS minSucc
        FROM appointment s1
        RIGHT JOIN appointment s2 ON s1.endDateTime < s2.startDateTime
            AND s1.radiologyroom_id = s2.radiologyroom_id
        WHERE s1.startDateTime >=  '2012-10-01 00:00:00'
            AND s1.radiologyroom_id =174
            AND s1.endDateTime <  '2013-01-01 00:00:00'
        GROUP BY myStart
        ORDER BY s1.startDateTime
    )s
WHERE NOT 
    EXISTS (
        SELECT NULL 
        FROM appointment
        WHERE startDateTime >= myStart
            AND endDateTime <= minSucc
            AND radiologyroom_id =174
        ORDER BY startDateTime
    )

它在14.6秒内从6530条记录中检索369行

如果id之间没有间隙,并且id总是在增加,您可以使用以下方法:

SELECT coalesce(s1.endDateTime, '0000-00-00 00:00:00'), s2.startDateTime
FROM
  slots s1 right join slots s2
  on s1.appointment_id=s2.appointment_id-1
WHERE coalesce(s1.endDateTime, '0000-00-00 00:00:00')<s2.startDateTime
LIMIT 10
编辑:您也可以尝试以下操作:

SELECT * FROM
  (SELECT
     coalesce(s1.endDateTime, '0000-00-00 00:00:00') as start,
     min(s2.startDateTime) minSucc
   from slots s1 right join slots s2
        on s1.endDateTime<s2.startDateTime
   group by start) s
WHERE
  not exists (select null
              from slots
              where startDateTime>=start
              and endDateTime<=minSucc)
EDIT2:我承认我对变量查询不太实际,但这看起来是可行的:

select d1, d2 from (
  select
    @previous_end as d1,
    s.startDateTime as d2,
    @previous_end:=s.endDateTime
  from (select startDateTime, endDateTime from slots order by startDateTime) s,
       (select @previous_end := '0000-00-00 00:00:00') t) s
where d1<d2

如果id之间没有间隙,并且id始终在增加,则可以使用以下方法:

SELECT coalesce(s1.endDateTime, '0000-00-00 00:00:00'), s2.startDateTime
FROM
  slots s1 right join slots s2
  on s1.appointment_id=s2.appointment_id-1
WHERE coalesce(s1.endDateTime, '0000-00-00 00:00:00')<s2.startDateTime
LIMIT 10
编辑:您也可以尝试以下操作:

SELECT * FROM
  (SELECT
     coalesce(s1.endDateTime, '0000-00-00 00:00:00') as start,
     min(s2.startDateTime) minSucc
   from slots s1 right join slots s2
        on s1.endDateTime<s2.startDateTime
   group by start) s
WHERE
  not exists (select null
              from slots
              where startDateTime>=start
              and endDateTime<=minSucc)
EDIT2:我承认我对变量查询不太实际,但这看起来是可行的:

select d1, d2 from (
  select
    @previous_end as d1,
    s.startDateTime as d2,
    @previous_end:=s.endDateTime
  from (select startDateTime, endDateTime from slots order by startDateTime) s,
       (select @previous_end := '0000-00-00 00:00:00') t) s
where d1<d2

遗憾的是,db的设计并没有受到这样的限制。我应该提到的。无论如何,谢谢你@ixM第二次查询仍应给出相同的结果,即使存在间隙或行未排序,但速度可能较慢。。。如果有重叠的时间间隔,它将不起作用,否则应该没问题。我已经修改了它,请参见编辑,它可以工作,但它非常慢,大约需要7秒才能完成10个结果。有什么办法可以改进吗?非常感谢@ixM我认为使用带有变量的查询应该快得多,但我不太实际。。。稍后我会尝试…@ixM请看一下我更新的查询,我使用的是变量,但我不确定它是否会更快…遗憾的是,db并不是根据这样的约束设计的。我应该提到的。无论如何,谢谢你@ixM第二次查询仍应给出相同的结果,即使存在间隙或行未排序,但速度可能较慢。。。如果有重叠的时间间隔,它将不起作用,否则应该没问题。我已经修改了它,请参见编辑,它可以工作,但它非常慢,大约需要7秒才能完成10个结果。有什么办法可以改进吗?非常感谢@ixM我认为使用带有变量的查询应该快得多,但我不太实际。。。稍后我将尝试…@ixM请查看我的更新查询,我使用的是变量,但我不确定它是否会更快。。。