0

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
  • You need ORDER BY to get the rows of a table in a certain order. Every other order is a coincidence. Commented Sep 17, 2022 at 14:28

1 Answer 1

1

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