views:

36

answers:

4

Hi everyone, I have two tables tbl_a and tbl_b and their fields are

tbl_a
-----
a_id a_name a_surname a_c_id a_d_id

tbl_b
-----
b_id b_name b_phone b_c_id b_d_id

I want to join these two table like this:

select *from tbl_a join tbl_b on tbl_a.a_c_id=tbl_b.b_c_id AND tbl_a.a_d_id=tbl_b.b_d_id where tbl_a.id>15;

As you can see i want to use two keys for ON keyword,is there anyway to do this? Thanks for advance...

A: 

Sql server allows this. I can speak for other implementations.

Carnotaurus
+1  A: 

The way you put it should work fine.

In MySQL you can set multiple join constraints by using AND.

AvatarKava
A: 

it works fine but use alias instead of long tablename

select * from tbl_a a 
         join tbl_b b on a.a_c_id=b.b_c_id AND a.a_d_id=b.b_d_id 
         where a.id>15;
Salil
+1  A: 

Your query is right there is no need for putting extra On keyword AND keyword do work of join in on two diffirent coluns. Just put the bracket to understand properly.

select *from tbl_a join tbl_b on 
(
  tbl_a.a_c_id=tbl_b.b_c_id 
  AND 
  tbl_a.a_d_id=tbl_b.b_d_id 
)
where tbl_a.id>15;
Pranay Rana
Thanks it works!I want to ask something else,in tbl_a there are basically around hundred records but in tbl_b there hundred thousands records and it takes almost 120 seconds to execute it.Is it normal?I use dedicated server
cubuzoa
you are welcome
Pranay Rana
I solved it there was logical error so it causes low performance.thank you again
cubuzoa