I tried to think of a sql query to get results consisting of multiple columns from multiple records, which the table is like:
key_1 key_2
--------------------
1 2
1 3
1 4
5 6
Both key_1 and key_2 are primary keys from another table. And the result I want to obtain is getting every key that are related to the key '1', which is:
key
--------
1
2
3
4
I tried the following query:
SELECT key_1 as key FROM tbl_keys_link WHERE key_1 IN (1)
UNION
SELEVY key_2 as key FROM tbl_keys_link WHERE key_2 IN (1)
But I examined it using pgadmin's 'Explain' function, seems the process time of this query is not very optimal. Is there any other ways to construct a query to achieve this with a better performance? Thanks!