合并MySQL SELECT中的列 [英] Merge columns in MySQL SELECT

查看:142
本文介绍了合并MySQL SELECT中的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储默认配置的表和一个存储用户配置的表.我可以将两个表连接起来并获取所有我需要的信息,但是我希望当第二列中存在值时,可能会有一种更干净的方法来覆盖另一列.

I have a table that stores a default configuration and a table that stores a user configuration. I can join the two tables and get all the info I need however I was hoping there might be a cleaner way to overwrite one column with the other when a value exists in the second column.

示例:

当前查询结果:

id  defaultValue   userValue
1       one           ONE
2       two
3       three        THREE
4       four 

期望查询结果:

id  value   
1   ONE
2   two
3   THREE
4   four 

也许没有一个很好的方法来执行此操作……我想问一下,因为如果存在某种方法,在MySQL中执行该操作可能比在PHP中执行该操作更快.

Maybe there isn't a good way to do this... Thought I'd ask though as it's probably faster to do it in MySQL if a method exists than to do it in PHP.

推荐答案

您可以使用

You can use COALESCE() for this:

SELECT id, COALESCE(uservalue,defaultvalue) AS value
FROM table

这篇关于合并MySQL SELECT中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆