sql - Where clause to check against two columns in another table -


i struggling answer reason.

i have 2 tables, table1 , table2 this:

table1:

id   location  warehouse 1    london    narnia 2    cyprus    metro 3    norway    neck 4    paris     triumph 

table2:

id   area      code 1    london    narnia 2    cyprus    metro 3    norway    triumph 4    paris     neck 

i need first select table1 table1.location in table2.area and table1.warehouse in table2.code given that table1.location in table2.area. i.e. want:

id   location  warehouse 1    london    narnia 2    cyprus    metro 

i have got to:

select   1.location , 1.warehouse table1 1 1.location in (select area table2) , 1.warehouse in (select code table2) 

but won't work because need second clause executed based on first clause holding true.

i have tried similar queries joins no avail.

is there simple way this?

use exists:

select t.location, t.warehouse table1 t exists (select 1               table2 t2               t.location = t2.area , t.warehouse = t2.code              ); 

i should point out databases support row constructors in. allows do:

select t.location, t.warehouse table1 t where(t1.location, t1.warehouse) in (select t2.area, t2.code table2 t2); 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -