当在表中找不到时,mysql将值映射到默认值

发布于 2024-12-16 13:03:47 字数 541 浏览 1 评论 0原文

我在 mySQL 中有这个表,旨在将某些值映射到其他值,例如国家/地区到首都。

我想这样使用它:

SELECT capital
FROM CountryData
WHERE country = 'France';

或者更确切地说,更新其他一些表:

UPDATE CustomerData
SET customerCapital =
   SELECT capital
   FROM CountryData
   WHERE CountryData.country = CustomerData.customerCountry

当在 Country 表中找不到该国家/地区时,就会出现问题。当 customerCountry 不存在时,如何将 customerCapital 设置为某个默认值 'UNKNOWN' 而不是 NULL CountryData 表?

I have this table in mySQL intended to map some values to other values, for example countries to capitals.

I want to use it this way:

SELECT capital
FROM CountryData
WHERE country = 'France';

or rather, to update some other table:

UPDATE CustomerData
SET customerCapital =
   SELECT capital
   FROM CountryData
   WHERE CountryData.country = CustomerData.customerCountry

The problem arises when the country is not found in the Country table. How can I have customerCapital set to some default value 'UNKNOWN' instead of NULL when customerCountry is not present in the CountryData table?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心作怪 2024-12-23 13:03:47

检查COALESCE 函数。

但是,我不建议您存储任意值而不是 NULL。除 NULL 之外的任何其他值本身就是一种信息。您想显示缺乏价值,因此您应该使用 NULL。
听起来像是美容问题。如果您想在将 customerCapital 打印到屏幕时显示一个值,那么您应该在应用程序代码中将 NULL 转换为 UNKNOWN

Check the COALESCE function.

However, I do not advise you to store an arbitrary value instead of NULL. Any other value than NULL is an information in itself. You want to show the lack of value, therefore you should use NULL.
It sounds like a cosmetic problem. If you want to show a value when you print the customerCapital to the screen, then you should, in the application code, transform the NULL into UNKNOWN.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文