0

Every five minutes, my system will produce a file and I will store the file name in my table.

I name the file name like:

  • studentA202106011940-202106011945.zip
  • studentA202106011945-202106011950.zip

So in one day, I will store 288 files in my table.

Now I can search the file in some day like:

select * 
from studentData 
where LEFT(SUBSTRING_INDEX(fileName, '-',-1),8)='29210601'

How can I quickly find the missing data from table?

10
  • 3
    Please define what "missing data" means. Sample data and desired results would really help. Commented Jun 9, 2021 at 12:00
  • 1
    Write a recursive query to generate all file names, then use NOT EXISTS to see which ones are missing. Commented Jun 9, 2021 at 12:02
  • How can I quickly find the missing data from table? "Missing data" is the filename which is absent for specified data? Commented Jun 9, 2021 at 12:04
  • Write a recursive query to generate all file names ,you means to create a temp table?@ThorstenKettner Commented Jun 9, 2021 at 12:16
  • 1
    No, MySQL 5.7 is old. It was MySQL 8 when they introduced this and several other features. It is worth upgrading :-) Commented Jun 9, 2021 at 14:30

2 Answers 2

3

You can check if the next starttime is not the same the a given rows end time. This identifies the first missing row in a sequence of adjacent missing rows:

with f as (
      select f.*,
             right(substring_index(filename, '-', 1), 12) as starttime,
             substring_index(substring_index(filename, '-', -1), '.', 1) as endtime
      from files f
     ) 
select f.*, f.endtime as first_missing_starttime
from (select f.*,
             lead(starttime) over (order by starttime) as next_startttime
      from f
     ) f
where f.next_starttime <> f.endtime;
Sign up to request clarification or add additional context in comments.

3 Comments

@flower . . . This requires MySQL 8+.
I try to rewrite your sql using view.
@flower . . . This logic would be very tricky -- and expensive -- to put into a view in pre-8.0 versions of MySQL. You might consider upgrading MySQL.
0

I'm sure this can be written more succinctly, but anyway...

For 5.7, I have a table of integers (i) from 0 to 9...

DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(my_string CHAR(12) NOT NULL PRIMARY KEY);

INSERT INTO my_table VALUES
('202106011945'),('202106011950');

SELECT SEC_TO_TIME((i4.i*1000+i3.i*100+i2.i*10+i1.i) *300) t
   FROM ints i1
   JOIN ints i2
   JOIN ints i3
   JOIN ints i4 
   LEFT
   JOIN my_table x
     ON TIME(STR_TO_DATE(CONCAT(x.my_string,00),'%Y%m%d%H%i%s')) = SEC_TO_TIME((i4.i*1000+i3.i*100+i2.i*10+i1.i) *300)
  WHERE i4.i*1000+i3.i*100+i2.i*10+i1.i <288
    AND x.my_string IS NULL;

+----------+
| t        |
+----------+
| 00:00:00 |
...
| 18:45:00 |
| 18:50:00 |
| 18:55:00 |
| 19:00:00 |
| 19:05:00 |
| 19:10:00 |
| 19:15:00 |
| 19:20:00 |
| 19:25:00 |
| 19:30:00 |
| 19:35:00 |
| 19:40:00 | -- we can see that
| 19:55:00 | -- it's working
| 20:00:00 |
| 20:05:00 |
...
+----------+

2 Comments

CREATE TABLE ints (i int(12) NOT NULL PRIMARY KEY); INSERT INTO ints VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9); I run the sql and the result is null
I don't think INT(12) is a thing. Anyway, you forgot 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.