MySql表定义为:
CREATE TABLE tbl_misure_30m (
m_rcd_id INT NOT NULL AUTO_INCREMENT ,
m_fon_id INT UNSIGNED,
m_timestamp TIMESTAMP,
m_fon_rcd_id INT UNSIGNED,
m_fon_Leq FLOAT(4,2),
m_fon_LsMax FLOAT(4,2),
m_Leq_state INT,
m_LsMax_state INT,
m_fon_mem_block INT,
INDEX fon_key (m_fon_id),
FOREIGN KEY (m_fon_id) REFERENCES tbl_users(fon_id)
ON DELETE CASCADE
ON UPDATE CASCADE,
PRIMARY KEY ( m_rcd_id )
) ENGINE = InnoDB;
其中:
不幸的是,数据库设计无法更改。
现在,我需要验证列m_fon_rcd_id(每个m_fon_id 的)不包含空洞(在这种情况下,我需要知道范围)。
我想要的是结果集如:| gap_starts_at | gap_ends_at | m_fon_id |
我找到this answer但是理所当然地认为各种ID都是唯一的:
SELECT (t1.m_fon_rcd_id + 1) as gap_starts_at,
(SELECT MIN(t3.m_fon_rcd_id) -1 FROM tbl_misure_30m t3 WHERE t3.m_fon_rcd_id > t1.m_fon_rcd_id) as gap_ends_at
FROM tbl_misure_30m t1
WHERE NOT EXISTS (SELECT t2.m_fon_rcd_id FROM tbl_misure_30m t2 WHERE t2.m_fon_rcd_id = t1.m_fon_rcd_id + 1 )
HAVING gap_ends_at IS NOT NULL
有什么建议吗?
提前致谢!
答案 0 :(得分:1)
我会通过获取下一个值来解决这个问题。然后对此做一些算术:
select m_fon_rcd_id + 1 as gapstart, next_m_fon_rcd_id - 1 as gap_ends,
(next_m_fon_rcd_id - m_fon_rcd_id - 1) as gap_length
from (select m_fon_rcd_id,
(select m2.m_fon_rcd_id
from tbl_misure_30m m2
where m2.m_fon_rcd_id > m.m_fon_rcd_id
order by m_fon_rcd_id
limit 1
) as next_m_fon_rcd_id
from tbl_misure_30m m
) m
where next_m_fon_rcd_id > m_fon_rcd_id + 1;
编辑:
如果您想在m_fon_id
中填写此差距,可以将其添加到查询的各个部分:
select m_fon_id, m_fon_rcd_id + 1 as gapstart, next_m_fon_rcd_id - 1 as gap_ends,
(next_m_fon_rcd_id - m_fon_rcd_id - 1) as gap_length
from (select m_fon_rcd_id,
(select m2.m_fon_rcd_id
from tbl_misure_30m m2
where m2.m_fon_id = m.m_fon_id and
m2.m_fon_rcd_id > m.m_fon_rcd_id
order by m_fon_rcd_id
limit 1
) as next_m_fon_rcd_id
from tbl_misure_30m m
) m
where next_m_fon_rcd_id > m_fon_rcd_id + 1;