Im trying to get all unique values out of two rows in a databank as a list. Using postgresql and python.
I use this sql query:
SELECT DISTINCT from,to FROM database;
I get this output
from | to
------+-----
a | b
d | c
b | a
And in python as: [('a', 'b'), ('d', 'c'), ('b', 'a')]
But i want the result:["a", "b", "d", "c"]
I know i can use python to get this result but is there a direct sql query for that?
2
Answers
I found a solution:
In postgresql the UNION operator already restricts to distinct elements if ALL is not added. The alias is used to ensure that the output table column is named "value".
You can make an union and select unique elements from there: