sql查询来像警报一样及时获取冲突

时间:2018-07-13 02:54:18

标签: mysql sql

我正在尝试创建一个SQL查询,以获取表中可能存在的冲突的列表,但到目前为止,我的SQL无法正常工作。 想法是具有报警功能

如果date_start = date_end表示一枪

示例:date_start = 2018-11-07 10:37:00date_end = 2018-11-07 10:37:00

如果date_start not nulldate _end = null,则天数栏应不同于0

示例:date_start = 2018-11-07 10:37:00 and date_end = NULL and Monday = 2 。 这意味着我将在每个星期一开始start_date

我现在的问题是,如果我有两个可以同时显示在同一时间和日期的行,我将尝试获取冲突列表。这是我的数据库的示例:

sampledb

我在上面创建了一个sql查询,但是运行不正常,任何人都可以帮忙吗?

SELECT
        id
    FROM
        my_table
    WHERE
        date_start IN(
        SELECT
            date_start
        FROM
            my_table
        WHERE
            date_start = date_end 
        GROUP BY
            date_start
        HAVING
            COUNT(*) > 1
    )
    UNION
    SELECT
        id
    FROM
        my_table
    WHERE
        DATE_FORMAT(date_start, '%H:%i:%s') IN(
        SELECT
            DATE_FORMAT(date_start, '%H:%i:%s')
        FROM
            my_table
        WHERE
            date_end IS NULL AND(
                monday = 2 OR tuesday = 3 OR wednesday = 4 OR thursday = 5 OR friday = 6 OR saturday = 7 OR sunday = 1
            )
        GROUP BY
            DATE_FORMAT(date_start, '%H:%i:%s')
        HAVING
            COUNT(*) > 1
    )
    UNION
    SELECT
        id
    FROM
        my_table
    WHERE
        DATE_FORMAT(date_start, '%H:%i:%s') IN(

        SELECT
            CASE WHEN
                        (DAYOFWEEK(date_start) = 2 AND (select monday from my_table where monday = 2) IS NOT NULL)
                     OR (DAYOFWEEK(date_start) = 3 AND (select tuesday from my_table where tuesday = 3) IS NOT NULL)
                     OR (DAYOFWEEK(date_start) = 4 AND (select wednesday from my_table where wednesday = 4) IS NOT NULL)
                     OR (DAYOFWEEK(date_start) = 5 AND (select thursday from my_table where thursday = 5) IS NOT NULL)
                     OR (DAYOFWEEK(date_start) = 6 AND (select friday from my_table where friday = 6) IS NOT NULL)
                     OR (DAYOFWEEK(date_start) = 7 AND (select saturday from my_table where saturday = 7) IS NOT NULL)
                     OR (DAYOFWEEK(date_start) = 1 AND (select sunday from my_table where sunday = 1) IS NOT NULL)
                 THEN DATE_FORMAT(date_start, '%H:%i:%s')
                 ELSE NULL
                 END
        FROM
            my_table
        WHERE
            (DAYOFWEEK(date_start) = 2
                                   OR DAYOFWEEK(date_start) = 3
                                   OR DAYOFWEEK(date_start) = 4
                                   OR DAYOFWEEK(date_start) = 5
                                   OR DAYOFWEEK(date_start) = 6
                                   OR DAYOFWEEK(date_start) = 7
                                   OR DAYOFWEEK(date_start) = 1
            )
        GROUP BY
            DATE_FORMAT(date_start, '%H:%i:%s')
        HAVING
            COUNT(*) > 1
    )

1 个答案:

答案 0 :(得分:0)

也许是这样的

SELECT date_start FROM alarms 
WHERE date_end IS NULL AND 
monday + tuesday + wednesday + thursday + friday + saturday + sunday > 0 
GROUP BY TIME(date_start) 
HAVING COUNT(monday)>0 OR COUNT(tuesday)>0 OR COUNT(wednesday)>0 OR 
COUNT(thursday)>0 OR COUNT(friday)>0 OR COUNT(saturday)>0 OR COUNT(sunday)>0

更新

如果您想捕获一次事件和每周事件之间的冲突,可以使用类似这样的查询

SELECT a1.date_start AS oneshow,a2.date_start AS weekly,DAYOFWEEK(a1.date_start) as dow 
FROM alarms AS a1
LEFT JOIN alarms AS a2 ON
  a1.date_start >= a2.date_start AND a2.date_end IS NULL
    AND TIME(a1.date_start) = TIME(a2.date_start)
    AND DAYOFWEEK(a1.date_start) IN (a2.monday, a2.tuesday, a2.wednesday, a2.thursday, a2.friday, a2.saturday, a2.sunday)
WHERE a1.date_end IS NOT NULL