【发布时间】:2014-12-02 09:41:19
【问题描述】:
我有两个表如下:
Table 1 "customer" with fields "Cust_id", "first_name", "last_name" (10 customers)
Table 2 "cust_order" with fields "order_id", "cust_id", (26 orders)
我需要显示"Cust_id" "first_name" "last_name" "order_id"
到我需要order_id group by cust_id 的计数,例如列出每个客户下的订单总数。
我在查询下方运行,但是,它正在计算所有 26 个订单并将这 26 个订单应用到每个客户。
SELECT COUNT(order_id), cus.cust_id, cus.first_name, cus.last_name
FROM cust_order, customer cus
GROUP BY cust_id;
您能否建议/建议查询中有什么问题?
【问题讨论】:
-
主要问题是您没有在查询中的两个表之间进行任何联接。
-
嗨 Siva,我运行下面的查询并加入表但给出相同的结果:SELECT COUNT(order_id), cus.cust_id, cus.first_name, cus.last_name FROM cust_order JOIN customer cus GROUP BY cust_id am i这里做错了什么?
-
你需要说出加入的类型。有关可能连接的详细信息,请参阅我的答案,以及说明它们如何连接的 ON 部分(查询在执行时不会真正查看关系,除非您告诉他们)
标签: mysql