加入报价单,订单和客户表?

时间:2014-04-02 07:59:40

标签: mysql join

我想加入3个表格,输出一行详细说明报价编号,客户名称和订单总数。

TBL_QUOTATIONS

quotation_id     |   customer_id   |    status     
-----------------+-----------------+---------------
1038                 21                 Open
1039                 22                 Open     

TBL_CUSTOMERS

customer_id      |   name          |    status     
-----------------+-----------------+---------------
21                  David               Active 
22                  Alvin               Active      

TBL_ORDERS

order_id     |   quote_id   |    desc             |    amount   
-------------+--------------+---------------------+-------------
1                 1038          Consultation           1500
2                 1038          Design Fees            1200
3                 1038          Misc Fees               500

需要帮助才能使声明正确执行上述操作...目前我正在使用

SELECT tbl_quotations.quote_id,customers.customer_id,name,sum(amount) FROM tbl_quotations INNER JOIN客户ON tbl_quotations.customer_id = tbl_customers.customer_id 加入订单ON tbl_quotations.quote_id = tbl_orders.quote_id

RESULT

quote_id     |   customer_name   |    sum(amount)             
-------------+-------------------+-------------------
1038              David              3200            
1039              Alvin                 0
 .                 .                   .
 .                 .                   .
 .                 .                   .

我不知道哪里出错了,但显然即使你在数据库中有更多的引号,该语句也只返回一行。

关于我哪里出错的任何帮助或建议?我的方法是否合适? 谢谢!

1 个答案:

答案 0 :(得分:1)

尝试

SELECT tbl_quotations.quote_id, customers.customer_id, name, sum(amount) 
FROM tbl_quotations INNER JOIN customers ON tbl_quotations.customer_id = tbl_customers.customer_id Join orders ON tbl_quotations.quote_id = tbl_orders.quote_id
GROUP BY tbl_quotations.quote_id
相关问题