postgresql - Query to match multiple column values with multiple rows -


i have following table in postgresql 9.1

table contact:

contact_id     phone     mobile 1               123       456 2               111       222 3               333       123 4               222       444 

table role:

contact_fk        exchange 7                 1 8                 2 1                 4 5                 5 2                 4 4                 5 

i need result like:

contact_id      phone    mobile      exchange 1               123       456         4 3               333       123         4 2               111       222         4 

i want contact data mobile field data in other contacts phone field , user must in exchange 4, available in contact_role table

fyi: contact table contains around 50k rows joining contact table taking lot time, must apply contact_role condition together.

joining 50k table 2 columns (where contact_fk primary) shouldn't take long time.

select t1.contact_id, t1.phone, t1.mobile, t2.exchange contact t1 join role t2 on t1.contact_id = t2.contact_fk t2.exchange = 4 

or if have index on exchange column, faster:

select t1.exchange, t2.contact_id, t2.phone, t2.mobile role t1 join contact t2 on t1.contact_fk = t2.contact_id t1.exchange = 4 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -