2

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 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.

1 Answer 1

6

You can use COALESCE() for this:

SELECT id, COALESCE(uservalue,defaultvalue) AS value
FROM table
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.