sql从一个表和另一个表中获取数据三列

时间:2013-08-15 19:49:38

标签: sql

我有两张桌子销售和客户 从第一个表我想总结两列recived_amount和bill_amount并减去它们两者之后也想得到customer_id

从第二个表我只想获得Customer_name

我试过

  SELECT Customer_details.CUS_name, 
       SUM(SALES.Bill_Amount) - SUM(SALES.Recived_Amount),
       Customer_details.Cus_id
  from sales 
  INNER JOIN Customer_details 
       ON Customer_details.Cus_id=sales.Cus_id
  where SALES.Cus_id = 1 order BY Cus_id

2 个答案:

答案 0 :(得分:1)

 SELECT Customer_details.CUS_name, (SUM(SALES.Bill_Amount) - SUM(SALES.Recived_Amount)) as       Subtract,Customer_details.Cus_id

 from sales INNER JOIN Customer_details ON Customer_details.Cus_id=sales.Cus_id

 where SALES.Cus_id = 1 

 group by Customer_details.CUS_name,Customer_details.Cus_id

 order BY Cus_id

答案 1 :(得分:0)

SELECT Customer_details.CUS_name,
       (SUM(SALES.Bill_Amount)-SUM(SALES.Recived_Amount)) AS Balance_Owed,
       Customer_details.Cus_ID
FROM Sales INNER JOIN Customer_details ON Customer_details.Cus_id=Sales.Cus_id
GROUP BY Customer_details.CUS_name,Customer_details.Cus_id
ORDER BY Customer_details.Cus_id;