I have a table in my database (postgres) Table #1
----------------------------------------------------
| name | id_a | id_b | id_c | id_d | id_e | id_f
----------------------------------------------------
I am now given a list of ids. Some of these ids belong to id_b, some to id_c and so on till id_f. For these list of ids I need to find the corresponding id_a.
Method #1 Identify which ids belong to id_b, id_c and so on. Then use multiple in clause in my mysql-query to fetch id_a
select id_a from Table #1 t1 where t1.id_b in () or t1.id_c in ().... or t1.id_f in ();
I am looking for an alternative way.
Method #2
Is there some way to query all the columns simultaneously for a given value. Something like
select id_a from Table #1 t1 where t1.id_b,t1.id_c,..t1.id_f in ();
In postgres, you can use the array-overlap, &&
, operator:
SELECT id_a
FROM mytable
WHERE ARRAY[id_b, id_c, id_d, id_e, id_f] && ARRAY[value1, value2, ...]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With