UNION 2表,(组合它们)

时间:2013-05-13 09:55:28

标签: mysql sql union

我有Products_A

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;

但这看起来没有格式化。详情

1 个答案:

答案 0 :(得分: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