繁体   English   中英

如果在UNION ALL查询中未找到任何行,则返回默认值

Return a default value if no rows are found in UNION ALL query

提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供   中文繁体   英文版本   中英对照 版本,有任何建议请联系yoyou2525@163.com。

脚本

我有一个从这个问题开发的查询,优化的一部分是创建一个MySql视图,用于为用户和销售生成统计信息,问题是当其中一个SELECT行没有结果时,它从结果表。

如果没有为任何SELECT找到行,如何告诉MySql设置默认值(例如0)?

这是用于创建视图的代码

CREATE OR REPLACE VIEW user_events AS
    SELECT 'Complete profiles' AS type, created_at
        FROM users
        WHERE completed_registration = 1

    UNION ALL SELECT 'Incomplete profiles', created_at
        FROM users
        WHERE completed_registration = 0 AND verified_email = 1

    UNION ALL SELECT 'Unverified profiles', created_at
        FROM users
        WHERE verified_email = 0

    UNION ALL SELECT 'Onsite Teachers', created_at
        FROM onsite_teachers

    UNION ALL SELECT 'Onsite Teachers hired', created_at
        FROM purchases
            INNER JOIN purchased_profiles
                ON purchased_profiles.purchase_id = purchases.id
                 AND purchased_profiles.profile_type = 'onsite_teacher'
        WHERE purchases.transaction_status = 'completed'

    UNION ALL SELECT 'Translators', created_at
        FROM translators

    UNION ALL SELECT 'Translators hired', created_at
        FROM purchases
            INNER JOIN purchased_profiles
                ON purchased_profiles.purchase_id = purchases.id
                AND purchased_profiles.profile_type = 'translator'
        WHERE purchases.transaction_status = 'completed'

    UNION ALL SELECT 'Interpreters', created_at
        FROM interpreters

    UNION ALL SELECT 'Interpreters hired', created_at
        FROM purchases
            INNER JOIN purchased_profiles
                ON purchased_profiles.purchase_id = purchases.id
                AND purchased_profiles.profile_type = 'interpreter'
        WHERE purchases.transaction_status = 'completed';

这是查询过去6个月(包括当月)总计的代码。

SELECT 
    type, 
    COUNT(CASE 
        WHEN created_at >= CAST(CURDATE() - INTERVAL (DAYOFMONTH(CURDATE()) - 1) DAY AS DATETIME) 
        THEN 1 
    END) AS 0_month_ago, 

    COUNT(CASE 
        WHEN created_at BETWEEN CAST(CURDATE() - INTERVAL (DAYOFMONTH(CURDATE()) - 1) DAY - INTERVAL 1 MONTH AS DATETIME)
            AND CAST(LAST_DAY(CURDATE() - INTERVAL 1 MONTH) + INTERVAL 1 DAY AS DATETIME)
        THEN 1 
    END) AS 1_month_ago,

    COUNT(CASE 
        WHEN created_at BETWEEN CAST(CURDATE() - INTERVAL (DAYOFMONTH(CURDATE()) - 1) DAY - INTERVAL 2 MONTH AS DATETIME)
            AND CAST(LAST_DAY(CURDATE() - INTERVAL 2 MONTH) + INTERVAL 1 DAY AS DATETIME)
        THEN 1 
    END) AS 2_months_ago,

    COUNT(CASE 
        WHEN created_at BETWEEN CAST(CURDATE() - INTERVAL (DAYOFMONTH(CURDATE()) - 1) DAY - INTERVAL 3 MONTH AS DATETIME)
            AND CAST(LAST_DAY(CURDATE() - INTERVAL 3 MONTH) + INTERVAL 1 DAY AS DATETIME)
        THEN 1 
    END) AS 3_months_ago,

    COUNT(CASE 
        WHEN created_at BETWEEN CAST(CURDATE() - INTERVAL (DAYOFMONTH(CURDATE()) - 1) DAY - INTERVAL 4 MONTH AS DATETIME)
            AND CAST(LAST_DAY(CURDATE() - INTERVAL 4 MONTH) + INTERVAL 1 DAY AS DATETIME)
        THEN 1 
    END) AS 4_months_ago,

    COUNT(CASE 
        WHEN created_at BETWEEN CAST(CURDATE() - INTERVAL (DAYOFMONTH(CURDATE()) - 1) DAY - INTERVAL 5 MONTH AS DATETIME)
            AND CAST(LAST_DAY(CURDATE() - INTERVAL 5 MONTH) + INTERVAL 1 DAY AS DATETIME)
        THEN 1 
    END) AS 5_months_ago
FROM 
    user_events
GROUP BY 
    type;

电流输出

如果你仔细观察,没有Interpreters hiredTranslators hired行,我想要设置这些行,如果它们返回null则归零

+=========================+===============+===============+===============+==============+===============+===============+
|          type           |  0_month_ago  |  1_month_ago  |  2_month_ago  | 3_month_ago  |  4_month_ago  |  5_month_ago  |
+=========================+===============+===============+===============+==============+===============+===============+
|    Complete profiles    |       7       |      20       |      14       |      25      |      30       |       7       |
+-------------------------+---------------+---------------+---------------+--------------+---------------+---------------+
|   Incomplete profiles   |      12       |      27       |      56       |      45      |      48       |      23       |
+-------------------------+---------------+---------------+---------------+--------------+---------------+---------------+
|   Unverified profiles   |       3       |      16       |      23       |       5      |       0       |       9       |
+-------------------------+---------------+---------------+---------------+--------------+---------------+---------------+
|    Onsite Teachers      |      11       |      36       |       8       |      15      |      46       |      12       |
+-------------------------+---------------+---------------+---------------+--------------+---------------+---------------+
|  Onsite Teachers hired  |       0       |       0       |      12       |       9      |       3       |       0       |
+-------------------------+---------------+---------------+---------------+--------------+---------------+---------------+
|      Interpreters       |       4       |      21       |      27       |      46      |      45       |      28       |
+-------------------------+---------------+---------------+---------------+--------------+---------------+---------------+
|      Translators        |       7       |      20       |      19       |      27      |      19       |      42       |
+-------------------------+---------------+---------------+---------------+--------------+---------------+---------------+
1 个回复

user_events视图是一种日志,看起来像

type                  | created_at
======================+==============
Interpreters hired    | 2014-12-12
Interpreters hired    | 2014-12-14
Interpreters hired    | 2014-12-16
Interpreters hired    | 2015-01-02

但是,如果没有雇用过口译员,那么就不会有type = 'Interpreters hired' 在这种情况下,计数查询无法凭空创建一个Interpreters hired行。

解决方案是确保在user_events视图中出现Interpreters hired行,无论如何。 您可以创建没有created_at日期的虚构行。 这样, GROUP BY总会有一些东西,但不一定是COUNT()

CREATE OR REPLACE VIEW user_events AS
    SELECT 'Complete profiles' AS type, created_at
        FROM users
        WHERE completed_registration = 1

    UNION ALL SELECT 'Incomplete profiles', created_at
        FROM users
        WHERE completed_registration = 0 AND verified_email = 1

    UNION ALL SELECT 'Unverified profiles', created_at
        FROM users
        WHERE verified_email = 0

    UNION ALL SELECT 'Onsite Teachers', created_at
        FROM onsite_teachers

    UNION ALL SELECT 'Onsite Teachers hired', created_at
        FROM purchases
            INNER JOIN purchased_profiles
                ON purchased_profiles.purchase_id = purchases.id
                 AND purchased_profiles.profile_type = 'onsite_teacher'
        WHERE purchases.transaction_status = 'completed'
    UNION ALL SELECT 'Onsite Teachers hired', NULL

    UNION ALL SELECT 'Translators', created_at
        FROM translators

    UNION ALL SELECT 'Translators hired', created_at
        FROM purchases
            INNER JOIN purchased_profiles
                ON purchased_profiles.purchase_id = purchases.id
                AND purchased_profiles.profile_type = 'translator'
        WHERE purchases.transaction_status = 'completed'
    UNION ALL SELECT 'Translators hired', NULL

    UNION ALL SELECT 'Interpreters', created_at
        FROM interpreters

    UNION ALL SELECT 'Interpreters hired', created_at
        FROM purchases
            INNER JOIN purchased_profiles
                ON purchased_profiles.purchase_id = purchases.id
                AND purchased_profiles.profile_type = 'interpreter'
        WHERE purchases.transaction_status = 'completed'
    UNION ALL SELECT 'Interpreters hired', NULL;
2 如果在UNION查询中未找到某些日期的记录,则返回0

我有以下查询 我得到的是每周的记录数量,出现在第一个子查询中且带有标签YES的记录数以及每周的记录数量出现在第二个子查询中且带有标签NO 。 例: 很多时候,第二个子查询不返回任何记录,因此某些日期我缺少行。 在上面的示例中,对于2/4/2019的NO行未返回任何内容 ...

3 如果在 JavaScript 中未找到任何内容,则返回 Firestore 查询

如果未找到 Firestore 中的搜索查询,我正在尝试使用 console.log “Not Found”。 目前,我正在使用以下内容执行查询,该查询在所有文档中查找电子邮件 =“123@test.com”。 如果文档包含我正在搜索的内容,这很好用,但如果没有找到任何东西,我就无法将它发送到 c ...

4 在查询中未找到任何值时,返回行

我有一个表MyTable ,它每天由两个外部机构( AGC1和AGC2 )的BizTalk填充。 我最近遇到了一个问题,其中AGC2已发送空白文件,因此没有记录得到更新。 我需要运行如下查询,这将返回 但是因为我没有AGC2记录,所以我只能得到 与第一个结果集一样,如何获 ...

5 如果在SQL查询中找不到结果,则返回默认值

我目前正在使用此查询从列关系和表媒体中选择“ profile_picture”,尽管我注意到我的html image标签中的return default.png不适用于结果。有什么方法可以返回:IF错误 "SELECT * FROM media WHERE post_id = '5552773 ...

2015-05-12 22:52:38 1 128   mysql/ sql
7 如果在对象中找不到任何内容,如何设置默认值

我试图显示产品清单,并希望显示该产品的销售数量。 此处no_of_sell具有product_id 。 问题是尚未售出的产品没有在no_of_sell中出售的编号,因此视图中显示空白空间。但是我想显示其0售出。 我该如何显示? 查看零件: (这里$product-&gt ...

8 如果未找到任何内容,则返回默认行

我有一个查询,该查询将返回数据,假设members.language_id大于零并支持一种语言。 如果成员没有选择语言,则members.language_id默认为零,这在连接另一个表时无济于事(请记住,我没有设计此表结构,也无法更改)。 所以,我想做的是没有结果返回默认值时出现以下情 ...

10 如果在日期期间没有可用的行,则SQL Server分组的行将返回默认值

我正在尝试编写一个存储过程,该存储过程根据行的月份对行进行分组,如果存在则返回所有项的总和,如果不存在则返回0。 对于查询的日期部分,我要获取的是今天的日期-提取月份,然后返回5个月以收集所有数据(如果存在)。 在此阶段,查询按原样运行良好,但是我想知道是否有任何方法可以对此进行优化 ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2022 STACKOOM.COM