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

时间:2012-12-11 11:29:19

标签: mysql sql user-variables

我正在尝试在MySQL数据库中选择约会之间的前十个空时隙。

约会表基本上有3个字段:appointment_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行

1 个答案:

答案 0 :(得分:1)

如果ids之间没有差距,且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