简体   繁体   English

MySQL的:两个表加总

[英]mysql: two table join with sum

I'm attempting to join two tables and also get a SUM and flailing badly. 我正在尝试联接两个表,并且还获得SUM并严重失败。 I need to get the total commission amounts for each affiliate where affiliate.approved=1 AND order.status=3. 我需要获取当affiliate.approved = 1 AND order.status = 3的每个会员的总佣金金额。

//affiliate table
affiliate_id | firstname | lastname | approved |
     1            joe        shmoe       1
     2           frank       dimag       0
     3            bob        roosky      1

here's the order table 这是订单表

//order
affiliate_id | order_status_id | commission
    1                3              0.20
    1                0              0.30
    2                3              0.10
    3                3              0.25
    1                3              0.25
    2                3              0.15
    2                0              0.20

and here's what I'd like the query to return: 这是我希望查询返回的内容:

affiliate_id | commission
    1             0.45
    3             0.25

Here is my attempt that doesn't work. 这是我的尝试,不起作用。 It outputs just one line. 它仅输出一行。

 SELECT order.affiliate_id, SUM(order.commission) AS total, affiliate.firstname, affiliate.lastname FROM `order`, `affiliate` WHERE order.order_status_id=3 AND affiliate.approved=1 AND order.affiliate_id = affiliate.affiliate_id ORDER BY total; 

thanks for any help. 谢谢你的帮助。

You can try this Query for your solution :- 您可以尝试此查询来解决您的问题:-

   SELECT order.affiliate_id, SUM(order.commission) AS total,affiliate.firstname,
  affiliate.lastname 
    FROM `order`, `affiliate`
    WHERE order.order_status_id=3 
    AND affiliate.approved=1 
    AND order.affiliate_id = affiliate.affiliate_id 
    GROUP BY order.affiliate_id
    ORDER BY total; 

You've missed GROUP BY , try this: 您错过了GROUP BY ,请尝试以下操作:

SELECT 
      `order`.affiliate_id,
      SUM(`order`.commission) AS total,
      affiliate.firstname,
      affiliate.lastname
FROM `order` 
JOIN `affiliate` 
ON `order`.order_status_id = 3 AND affiliate.approved = 1 AND `order`.affiliate_id = affiliate.affiliate_id
GROUP BY `order`.affiliate_id
ORDER BY total;

Demo Here 在这里演示

First: Remove the implicit join syntax. 第一:删除隐式join语法。 It's confusing. 令人困惑。

Second: You needed to group by affiliate_id . 第二:您需要按affiliate_id分组。 Using aggregate function without group by collapses your result set into a single row. 在不使用分组依据的情况下使用聚合函数会将结果集折叠为单行。

Here's the query using INNER JOIN : 这是使用INNER JOIN的查询:

SELECT
    `order`.affiliate_id,
    SUM(`order`.commission) AS total,
    affiliate.firstname,
    affiliate.lastname
FROM `order`
INNER JOIN`affiliate` ON  `order`.affiliate_id = affiliate.affiliate_id
WHERE `order`.order_status_id = 3
AND affiliate.approved = 1
GROUP BY affiliate.affiliate_id
ORDER BY total;

WORKING DEMO 工作演示

Caution: You have picked one of the reserved words of MySQL as table name ( order ). 警告:您已选择MySQL的保留字之一作为表名( order )。 Be aware to enclose it with (`)backtick always . 请注意,请始终使用(`)backtick将其括起来。

Just a gentle reminder 只是一个温柔的提醒

Here is the solution: 解决方法如下:

select affiliate.affiliate_id,sum(`order`.commission) as total from affiliate left join `order` on affiliate.affiliate_id=`order`.affiliate_id
where affiliate.approved=1 and `order`.order_status_id=3 group by affiliate.affiliate_id

In addition,"order" is a key word of SQL , I recommend you not to use it as a table/column name. 另外,“ order”是SQL的关键字,我建议您不要将其用作表/列名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM