Jump to content
  • Sign Up

SQL запрос - узнать отсутвующие записи


rgl

Recommended Posts

Понятно, что все что я хочу, легко делается с помощью языка программирования, напр. PHP, но ради спортивного интереса, можно ли это сделать одним запросом на SQL?

Итак, есть таблица с некими данными, одно из полей - дата-время. Хочется получить список дат (за заданный небольшой промежуток времени), для которых записей в таблице нет.

Напр. есть записи

"'2020-12-01 15:25:26", другие поля"

'2020-12-02 12:42:11", другие поля

'2020-12-04 11:33:42", другие поля

'2020-12-05 05:24:36", другие поля

'2020-12-07 07:15:56", другие поля

'2020-12-08 03:11:27", другие поля

Я хочу сделать запрос, задав исходный диапазон "2020-12-01" "2020-12-07" и получить результат "2020-12-03", "2020-12-06"

 

Edited by rgl
Link to post
Share on other sites

можно сделать вложенные select по каждому из периодов дат:
select
(select count(`date`) from `table` where `date`<='2020-12-01 23:59:59' and `date`>='2020-12-01 00:00:00') as `date2020-12-01`, 
(select count(`date`) from `table` where `date`<='2020-12-02 23:59:59' and `date`>='2020-12-02 00:00:00') as `date2020-12-02`,
....

ну и получится по тем датам где запрос вернет 0 там нет записей
можно сделать цикл перебора средствами mysql почитать можно тут
https://habr.com/ru/post/150491/

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Member Statistics

    47,199
    Total Members
    1,451
    Most Online
    тим
    Newest Member
    тим
    Joined
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...