MYSQL别名加入“不存在”

时间:2019-02-28 22:16:02

标签: mysql

我有两个桌子。在 TableNames 中,存储了几首歌曲的库。在 TableTimes 中,将存储播放歌曲的确切日期和时间。

在每个新的一周,我都会检查是否有从未播放过的新歌曲。简单。但是我的问题如下:我需要知道是在12:00:00(24h)之前还是之后播放歌曲。因此,我正在运行 CASE WHEN ,并输出值为 + -的别名 TimeCase ,请参阅查询下面。 如果实际一周中有新的歌曲<< strong> TimeCase ,我的查询应该输出。您可以在下面找到ID为101的示例。

ID 101在201908星期不是新的,但是它在12:00:00之后第一次播放,因此需要在我的查询输出中。

表名

+-------+-----+
| Name  | id  |
+-------+-----+
| Song1 | 100 |
+-------+-----+
| Song2 | 101 |
+-------+-----+
| Song3 | 102 |
+-------+-----+
| Song4 | 103 |

TableTimes:

+--------+--------+------+
| Week   | Time   | idFI |
+--------+--------+------+
| 201908 | 08:00  | 100  |
+--------+--------+------+
| 201908 | 19:00  | 101  |
+--------+--------+------+
| 201907 | 09:00  | 101  |
+--------+--------+------+
| 201906 | 22:00  | 103  |

我的查询

SELECT t2.idFI, t1.id, Name, TimeCase
    FROM TableTimes t2
    JOIN TableNames t1 ON t2.idFI = t1.id
    JOIN (
        SELECT idFI,
    CASE WHEN Time < '12:00:00' THEN '-'
        ELSE '+'
        END AS TimeCase
    FROM TableTimes GROUP BY idFI, TimeCase
    ) t3 ON t2.idFI = t3.idFI
    WHERE '201908' = Week
    AND deleteSZ = false
    AND   NOT EXISTS
            (
            SELECT  null
            FROM    TableTimes t5
                JOIN (
                    SELECT idFI,
                CASE WHEN Time < '12:00:00' THEN '-'
                    ELSE '+'
                    END AS TimeCaseBefore
                FROM TableTimes GROUP BY idFI, TimeCaseBefore
                ) t4 ON t5.idFI = t4.idFI
            WHERE   t5.idFI = t2.idFI
            AND TimeCaseBefore = TimeCase
            AND     Week<'201908'
            AND     deleteSZ = false
            GROUP BY t5.idFI, TimeCaseBefore)
    GROUP BY TimeCase, t2.idFI

哪个给我以下内容:

+-------+-----+------+----------+
| Name  | id  | idFI | TimeCase |
+-------+-----+------+----------+
| Song1 | 100 | 100  | -        |
+-------+-----+------+----------+

我真正想要的是:

+-------+-----+------+----------+
| Name  | id  | idFI | TimeCase |
+-------+-----+------+----------+
| Song1 | 100 | 100  | -        |
+-------+-----+------+----------+
| Song2 | 101 | 101  | +        |
+-------+-----+------+----------+

我对我的问题的理解是,我应该能够将 t5.idFI = t2.idFI 更改为 t5.TimeCaseBefore = t2.TimeCase 由于别名。

我该如何解决?

基于P.Salmon的最终解决方案:

select   tn.name,
            idfi,
            case when thiswkafter12 > 0 and priorwkafter12 = 0 and thiswkbefore12 > 0 and priorwkbefore12 = 0 then '+ -'
                 when thiswkafter12 > 0 and priorwkafter12 = 0 and priorwkbefore12 = 0 then '+'
                 when thiswkbefore12 > 0 and priorwkbefore12 = 0 and priorwkafter12 = 0 then '-'
            end as timecase
from
(
SELECT  idfi,
            sum(case when week = 201908 and time >= '12:00:00' then 1 else 0 end) as thiswkafter12,
            sum(case when week = 201908 and time <  '12:00:00' then 1 else 0 end) as thiswkbefore12,
            sum(case when week < 201908 and time >= '12:00:00' then 1 else 0 end) as priorwkafter12,
            sum(case when week < 201908 and time < '12:00:00' then 1 else 0 end) as priorwkbefore12
FROM TABLETIMES
group by idfi
having (thiswkafter12 > 0 and priorwkafter12 = 0 and thiswkbefore12 > 0 and priorwkbefore12 = 0) or
       (thiswkafter12 > 0 and priorwkafter12 = 0 and priorwkbefore12 = 0) or
       (thiswkbefore12 > 0 and priorwkbefore12 = 0 and priorwkafter12 = 0)
        ) s
join tablenames tn on tn.id = s.idfi    ;

1 个答案:

答案 0 :(得分:1)

也许是一种比较简单的方法,可以计算出本周播放歌曲的频率,并与前几周的播放频率(分为上午和下午)进行比较。我认为您的示例101的倒数也可以适用,即本周上午在某首歌播放但之前在下午某首歌播放的情况。

请注意,我已添加了本周上午和下午首次播放的歌曲

+--------+----------+------+
| Week   | Time     | idFI |
+--------+----------+------+
| 201908 | 08:00:00 |  100 |
| 201908 | 19:00:00 |  101 |
| 201907 | 09:00:00 |  101 |
| 201906 | 22:00:00 |  103 |
| 201908 | 08:00:00 |  104 |
| 201908 | 13:00:00 |  104 |
| 201908 | 08:00:00 |  104 |
| 201908 | 13:00:00 |  104 |
+--------+----------+------+
8 rows in set (0.00 sec)

   select   tn.name,
                idfi,
                case when thiswkafter12 > 0 and priorwkafter12 = 0 then '+'
                      when thiswkbefore12 > 0 and priorwkbefore12 = 0 then '-'
                end as timecase
    from
    (
    SELECT  idfi,
                sum(case when week = 201908 and time >= '12:00:00' then 1 else 0 end) as thiswkafter12,
                sum(case when week = 201908 and time <  '12:00:00' then 1 else 0 end) as thiswkbefore12,
                sum(case when week < 201908 and time >= '12:00:00' then 1 else 0 end) as priorwkafter12,
                sum(case when week < 201908 and time < '12:00:00' then 1 else 0 end) as priorwkbefore12
    FROM TABLETIMES
    group by idfi
    having (thiswkafter12 > 0 and priorwkafter12 = 0) or
             (thiswkbefore12 > 0 and priorwkbefore12 = 0)
    ) s
    join tablenames tn on tn.id = s.idfi    ;

+-------+------+----------+
| name  | idfi | timecase |
+-------+------+----------+
| Song1 |  100 | -        |
| Song2 |  101 | +        |
| song5 |  104 | +        |
+-------+------+----------+
3 rows in set (0.00 sec)

您还没有说过104会发生什么,所以我假设下午获胜。