How can I replace all null values in the result set with values from another row of the same table? (like a fallback query)
Example schema:
CREATE TABLE parent (
id INTEGER NOT NULL AUTO_INCREMENT,
int1 INTEGER,
int2 INTEGER,
int3 INTEGER,
PRIMARY KEY (id)
)
The query:
SELECT * FROM table1
WHERE id = ?
But I need to replace all null values with those of another row. I am looking for something like this:
SELECT * FROM table1 WHERE id = ?
REPLACE ALL NULL VALUES WITH (
SELECT * FROM table1 WHERE id = ?
)
Example:
id int1 int2 int3
---------------------------
1 1 null 1
2 null null 1
3 1 4 0
When I query for id 1 first and id 3 as fallback, I expect the result to be:
id int1 int2 int3
---------------------------
1 1 4 1
CASEstatement help you?