如果没有找到行,则返回默认值

编程入门 行业动态 更新时间:2024-10-28 14:36:39
本文介绍了如果没有找到行,则返回默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在构建查询以从数据库中搜索多行,例如

I'm building query to search multiple rows from database like

SELECT * FROM table1 WHERE col1 = '012311' OR col1 = '0123631' OR col1 = '091233' OR col1 = '092111'

这里它返回前 3 个值,因为它们存在于表中,但最后一个不返回任何值,因为它不在表中.那么如果没有找到该值的行,我该如何在查询中设置我的默认值?

Here it returns first 3 values because they are present in the table but it returns nothing for last one because it's not in table. So how can I set my default value in the query if no row is found for that value?

推荐答案

有几种方法,取决于数据集.这是一种方法:

There are several approaches, which depend on the dataset. Here's one way:

SELECT *, IFNULL( ( SELECT col1 FROM table1 WHERE col1 IN ('012311','0123631','091233','092111') ), 'some_value' ) AS my_col1 FROM table1;

不一定要复制+粘贴,您必须根据具体情况进行调整.

Not neccessarily copy+paste, you will have to adjust for your specific case.

更多推荐

如果没有找到行,则返回默认值

本文发布于:2023-10-25 22:35:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1528314.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如果没有   默认值

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!