Join Multiple tables in oracle sql

左心房为你撑大大i 提交于 2019-12-12 06:18:45

问题


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


回答1:


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" ;



回答2:


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';


来源:https://stackoverflow.com/questions/28020544/join-multiple-tables-in-oracle-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!