2012-07-06 102 views
-4

我有兩個表如何從兩個表中獲取所有數據?

1.產品

prod_id prod_name 
    1  honda 
    2  hero 
    3  marcedes 
    4  audi 

二,產品品類

cat_id prod_id 
    1   1 
    1   2 
    2   3 
    2   4 

現在我想導致像下面

prod_id prod_name cat_id 
    1   honda  1 
    2   hero   1 
    3   marcedes  2 
    4   audi   2 

我似乎無法人物這一點。任何幫助非常感謝!

+0

一個好的開始表聯接在這裏形成http://blog.sqlauthority.com/2009/04/13/sql-server-introduction-to-joins-basic-of-joins/ – Gokul 2012-07-06 11:36:06

回答

5
select products.*, category.cat_id 
from products, category 
where products.prod_id = category.prod_id 
2
select products.prod_id ,products.prod_name, category.cat_id 
from products, category 
where category.prod_id = products.prod_id 
+3

這樣一個有着如此高聲譽的小孩子。要走的路! L0L! – alfasin 2012-07-06 09:59:39

0

只需使用一個連接,如果你只想要的產品有CAT_ID,然後更改LEFT JOININNER JOIN

SELECT t1.prod_id, t1.prod_name, t2.cat_id 
FORM Products t1 
LEFT JOIN ProductToCategory t2 ON t1.prod_id = t2.prod_id 
2
select p.prod_id,p.prod_name,c.cat_id 
from Products p inner join category c 
on p.prod_id = c.prod_id 
0

選擇p.prod_id,p.prod_name,從商品P c.catid

內部聯接prodtocat PC上p.prod_id = pc.prod_id

相關問題