Join Multiple tables in oracle sql

2019-09-04 05:24发布

im new to SQL and want to know how to replace the below code with SQL joins.

I want to list all information based on p_id ='123'.

select p.p_name,c.c_name,s.s_name,s.s_contact,b.b_name,b.b_contact 
from product p, category c, seller s, buyer b 
where p.p_id="123" and c.p_id="123" and s.p_id="123" and b.p_id="123";

Tables used

Product Table

p_id
p_name

Category Table

p_id
c_id
c_name

Seller Table

p_id
s_id
s_name
s_contact

Buyer Table

p_id
b_id
b_name
b_contact

Thanks

2条回答
\"骚年 ilove
2楼-- · 2019-09-04 05:37

This is the query using join:

select p.p_name,c.c_name,s.s_name,s.s_contact,b.b_name,b.b_contact 
from product p 
join buyer b  on p.p_id = b.p_id and <second condition>
join category c on  p.p_id = c.p_id
join seller s on c.p_id  = s.p_id
where p.p_id="123" ;
查看更多
你好瞎i
3楼-- · 2019-09-04 05:38

Try joining all the table with your criteria as below:

SELECT p.p_name,c.c_name,s.s_name,s.s_contact,b.b_name,b.b_contact 
FROM product p INNER JOIN category c
ON   p.p_id = c.p_id
INNER JOIN seller s
ON p.p_id = s.p_id
INNER JOIN buyer b 
ON p.p_id = b.p_id
WHERE p.p_id='123';
查看更多
登录 后发表回答