2013-05-13 55 views
-1

我Products_AUNION 2個表,(將它們結合起來)

Product_ID Product_Name Price Quantity 
11   MilkA 36  56 3 
21   MeatB 123 78 23 
31   SugarA 29 45 333 

Products_B

Product_ID Product_Name Price Quantity 
21   MilkB 63  65 33 
22   MeatB 321 87 4345 
23   SugarB 92 54 232 

我想選擇這樣的查詢

Product_ID Quantity Quantity*Price 
11   36  
21   123 
31   29 
21   63 
22   321 
23   92 

我嘗試

SELECT 
    Products_A.Quantity, 
    Products_B.Quantity, 
    Products_A.Quantity * Products_A.Price, 
    Products_B.Quantity*Products_B.Price 
    FROM products_A, 
    products_B; 

但是這看起來沒有格式化。詳細信息

+0

在SQL查詢中搜索UNION! – goseo 2013-05-13 10:00:25

回答

2

如果您想要兩個表的結果,可以使用UNION ALL。

SELECT 
    a.Product_Id as product_Id, 
    a.Quantity, 
    a.Quantity * a.Price as total 
FROM Products_A a 
UNION ALL 
SELECT 
    b.Product_Id as product_Id, 
    b.Quantity, 
    b.Quantity * b.Price as total 
FROM Products_B b 
+0

謝謝拉斐爾,你忘記了B中的逗號,但這是爲未來的訪客。 – user2377214 2013-05-13 13:06:38