• <bdo id='aZoUx'></bdo><ul id='aZoUx'></ul>

      <small id='aZoUx'></small><noframes id='aZoUx'>

      <legend id='aZoUx'><style id='aZoUx'><dir id='aZoUx'><q id='aZoUx'></q></dir></style></legend>
      <tfoot id='aZoUx'></tfoot>
      1. <i id='aZoUx'><tr id='aZoUx'><dt id='aZoUx'><q id='aZoUx'><span id='aZoUx'><b id='aZoUx'><form id='aZoUx'><ins id='aZoUx'></ins><ul id='aZoUx'></ul><sub id='aZoUx'></sub></form><legend id='aZoUx'></legend><bdo id='aZoUx'><pre id='aZoUx'><center id='aZoUx'></center></pre></bdo></b><th id='aZoUx'></th></span></q></dt></tr></i><div id='aZoUx'><tfoot id='aZoUx'></tfoot><dl id='aZoUx'><fieldset id='aZoUx'></fieldset></dl></div>

        如果未找到单行,则返回默认值

        Return a default value if single row is not found(如果未找到单行,则返回默认值)

        <small id='BbhEp'></small><noframes id='BbhEp'>

      2. <tfoot id='BbhEp'></tfoot>
            <bdo id='BbhEp'></bdo><ul id='BbhEp'></ul>

                <tbody id='BbhEp'></tbody>

              <legend id='BbhEp'><style id='BbhEp'><dir id='BbhEp'><q id='BbhEp'></q></dir></style></legend>
                <i id='BbhEp'><tr id='BbhEp'><dt id='BbhEp'><q id='BbhEp'><span id='BbhEp'><b id='BbhEp'><form id='BbhEp'><ins id='BbhEp'></ins><ul id='BbhEp'></ul><sub id='BbhEp'></sub></form><legend id='BbhEp'></legend><bdo id='BbhEp'><pre id='BbhEp'><center id='BbhEp'></center></pre></bdo></b><th id='BbhEp'></th></span></q></dt></tr></i><div id='BbhEp'><tfoot id='BbhEp'></tfoot><dl id='BbhEp'><fieldset id='BbhEp'></fieldset></dl></div>

                  本文介绍了如果未找到单行,则返回默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有以下选择语句来获取流的下一个计划项目.如果没有匹配的行,我希望它返回一个默认值.这是我正在使用的 SQL:

                  I have the following select statement to grab the next scheduled item for a stream. If there is no matching row, I want it to return a default value. Here's the SQL that I'm using:

                  SELECT `file`
                  FROM `show`, `schedule` 
                  WHERE `channel` = 1
                    AND `start_time` <= UNIX_TIMESTAMP() 
                    AND `start_time` > UNIX_TIMESTAMP()-1800
                    AND `show`.`id` = `schedule`.`file` 
                  ORDER BY `start_time`
                  DESC LIMIT 1
                  

                  这应该抓取最近安排的项目,但如果它早于查询前 30 分钟,则不会.

                  That should grab the most recently scheduled item, but not if it's older than 30 minutes before the query.

                  但是,如果用户没有安排任何事情,我想要一个默认值,以便在流中实际播放某些内容.我尝试了以下方法:

                  However, if the user doesn't schedule anything, I want a default value, so that something actually plays on the stream. I've tried the following:

                  SELECT COALESCE(`file`, 'default.webm')
                  FROM `show`, `schedule`...
                  

                  SELECT IFNULL(`file`, 'default.webm')
                  FROM `show`, `schedule`
                  

                  然而,如果没有找到行,它总是返回一个空结果.我如何才能返回默认值?

                  However, it always returns an empty result if no rows are found. How can I return a default value instead?

                  推荐答案

                  一种方法

                  SELECT IFNULL(MIN(`file`), 'default.webm') `file` 
                    FROM `show`, `schedule` 
                   WHERE `channel` = 1 AND `start_time` <= UNIX_TIMESTAMP() 
                     AND `start_time` > UNIX_TIMESTAMP()-1800 AND `show`.`id` = `schedule`.`file` 
                   ORDER BY `start_time` DESC LIMIT 1
                  

                  因为你只返回一行,你可以使用聚合函数,在这种情况下MIN(),确保你在没有选择任何记录时得到NULL.然后 IFNULL()COALESCE() 将完成它的工作.

                  Since you return only one row, you can use an aggregate function, in that case MIN(), that ensures that you'll get NULL if no records selected. Then IFNULL() or COALESCE() will do its job.

                  这篇关于如果未找到单行,则返回默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Bogus foreign key constraint fail(虚假外键约束失败)
                  how to get last insert id after insert query in codeigniter active record(如何在codeigniter活动记录中插入查询后获取最后一个插入ID)
                  Force InnoDB to recheck foreign keys on a table/tables?(强制 InnoDB 重新检查表/表上的外键?)
                  How to auto generate migrations with Sequelize CLI from Sequelize models?(如何使用 Sequelize CLI 从 Sequelize 模型自动生成迁移?)
                  Clear MySQL query cache without restarting server(无需重启服务器即可清除 MySQL 查询缓存)
                  ALTER TABLE to add a composite primary key(ALTER TABLE 添加复合主键)
                  <i id='MFPAx'><tr id='MFPAx'><dt id='MFPAx'><q id='MFPAx'><span id='MFPAx'><b id='MFPAx'><form id='MFPAx'><ins id='MFPAx'></ins><ul id='MFPAx'></ul><sub id='MFPAx'></sub></form><legend id='MFPAx'></legend><bdo id='MFPAx'><pre id='MFPAx'><center id='MFPAx'></center></pre></bdo></b><th id='MFPAx'></th></span></q></dt></tr></i><div id='MFPAx'><tfoot id='MFPAx'></tfoot><dl id='MFPAx'><fieldset id='MFPAx'></fieldset></dl></div>
                  • <bdo id='MFPAx'></bdo><ul id='MFPAx'></ul>
                    • <tfoot id='MFPAx'></tfoot>

                            <tbody id='MFPAx'></tbody>

                          <small id='MFPAx'></small><noframes id='MFPAx'>

                            <legend id='MFPAx'><style id='MFPAx'><dir id='MFPAx'><q id='MFPAx'></q></dir></style></legend>