当在表中找不到时,mysql将值映射到默认值
我在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查
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
.