开发者

Merge columns in MySQL SELECT

开发者 https://www.devze.com 2023-02-19 12:19 出处:网络
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

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.

Example:

Current query result:

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

Desire query result:

id  value   
1   ONE
2   two
3   THREE
4   four 

Maybe there isn't a good way to do 开发者_Go百科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
0

精彩评论

暂无评论...
验证码 换一张
取 消