嘿伙计们,我有这个问题。
"SELECT COUNT(purchase_log.id), purchase_log.date_purchased, purchase_log.total_cost, purchase_log.payment_status, cart_contents.product_name, members.first_name, members.last_name, members.email FROM `purchase_log` LEFT JOIN `cart_contents` ON purchase_log.id = cart_contents.purchase_id LEFT JOIN `members` ON purchase_log.member_id = members.id";
正如你所看到的,我有3个不同的表,我试图从中提取数据。运行此查询后,它返回计数为9.这实际上是表“cart_contents”中的总行数,但我想要的是它返回5,因为表“purchase_log”包含每行的总事务数和每个事务可以在表“cart_contents”中包含1行或更多行。
那么如何构建查询以计算正确的金额?
答案 0 :(得分:0)
你可以尝试,
SELECT COUNT(DISTINCT purchase_log.id) purchase_log.date_purchased, purchase_log.total_cost, purchase_log.payment_status, cart_contents.product_name, members.first_name,members.last_name, members.email FROM
purchase_log
LEFT 加入cart_contents
ON purchase_log.id = cart_contents.purchase_id LEFT JOINmembers
ON purchase_log.member_id = members.id