SQL default row values

Viewed 19

If I have a table containing some data such as:

ID some_field1
0 this is a default row with default configuration
1 user configured this field

Would SELECT whatever FROM table WHERE id=? OR id=0 LIMIT 1; always return the configured row first unless it doesn't exist?

Additionally is there a way that could check if some_field1 is configured independently of other fields without writing all the default values? Say id1 has some_field1 as null but some_field2 configured to something custom, this query would return null instead of the default unless I use the DEFAULT VALUE in the schema creation (and keep that up to date with the row in the database).

I know it could be implemented in the program logic fairly easily but I was wondering if there is a one query solution in SQL itself.

1 Answers

If ? is null this query return whatever entity id = 0 otherwise it return entity id =?

SELECT whatever FROM table WHERE id= coalesce(?,0) LIMIT 1;
Related