Background
In MySQL, you can get the result of comparison operator as integer value like below. This feature is useful when you want to sort the result of SELECT with weighed matching score.
CREATE TABLE Table1 (`value1` int, `value2` int, `value3` int);
INSERT INTO Table1 (`value1`, `value2`, `value3`)
VALUES (1, 2, 3),
(2, 4, 6),
(3, 6, 9);
select value1, value2, value3, 1*(value1=1), 2*(value2=4), 3*(value3=9) from table1;
-> 1 2 3 1 0 0
2 4 6 0 2 0
3 6 9 0 0 3
Problem
In PostgreSQL, the result of comparison operator is boolen.
CREATE TABLE Table1 ("value1" int, "value2" int, "value3" int);
INSERT INTO Table1 ("value1", "value2", "value3")
VALUES (1, 2, 3),
(2, 4, 6),
(3, 6, 9);
select value1, value2, value3, value1=1, value2=4, value3=9 from table1;
-> 1 2 3 true false false
2 4 6 false true false
3 6 9 false false true
And you will get an error if you try to convert boolean value to integer:
select value1, value2, value3, 1*(value1=1), 2*(value2=4), 3*(value3=9) from table1;
ERROR: operator does not exist: integer * boolean Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts. Position: 33
Question
How can I get integer value as result of comparison operator in PostgreSQL?