简体   繁体   English

加入MySql表和SUM

[英]Join MySql Tables and SUM

I'm trying to write JOIN Query with 3 Tables. 我正在尝试用3个表编写JOIN查询。 I need Query to Return Total SUM of each users based Link Value Passed to Query. 我需要查询以返回基于传递给查询的链接值的每个用户的总SUM。 Added below the Table Structure, required result and Query that i tried. 在表结构,必需的结果和我尝试过的查询下面添加了。

Table 1: UserDetails 表1:用户详细信息


|   ID    | Name    | Link    |   
|   1     | User A  |    12   |
|   2     | User B  |    12   |
|   3     | User C  |    12   |

Table 2: Saving 表2:保存


|CreatedBy| Amount  |
|   3     | 100     | 
|   3     | 50      | 
|   2     | 75      | 

Table 3: Expense 表3:费用


|CreatedBy| Amount  |
|   2     | 20      | 
|   1     | 15      | 
|   3     | 85      | 

By Passing Link Value to Query must return Total(savings+expense) for each user. 通过将链接值传递给查询,必须为每个用户返回总计(储蓄+费用)。

Result 1 : If Passing Link Value 12. 结果1 :如果传递链接值12。

Query Result be 查询结果为


|User Name| Total   |
|User A   | 15      | 
|User C   | 235     | 

Result 2 : If Passing Link Value 11. 结果2 :如果传递链接值11。

Query Result be 查询结果为


|User Name| Total   |
|User B   | 95      | 

Below query Not returning any result. 下面的查询不返回任何结果。

SELECT 
    t1.name, 
    SUM(t2.total_amount+t3.total_amount) 
FROM 
    userdetails t1 
        LEFT JOIN savings t2 
            ON t2.created_by=t1.id 
        LEFT JOIN expense t3 
            ON t3.created_by=t1.id 
WHERE 
    t1.link=12 
    AND t1.status=1 
    AND t2.status=2 
    AND t3.status=2 
GROUP BY 
    t2.created_by,
    t3.created_by

When you make an outer join you cannot place conditions in the where clause otherwise it nullifies the outer join and turns it into a normal inner join. 进行外部联接时,不能在where子句中放置条件,否则它将使外部联接无效并将其转换为普通的内部联接。 You can however get what you want (based on your where clause) by adding the condition into the join like this: 但是,可以通过将条件添加到联接中来获得所需的内容(基于where子句),如下所示:

SELECT 
    t1.name, 
    SUM(t2.total_amount+t3.total_amount) 
FROM 
    userdetails t1 
        LEFT JOIN savings t2 
            ON t2.created_by=t1.id 
            AND t2.status=2 
        LEFT JOIN expense t3 
            ON t3.created_by=t1.id 
            AND t3.status=2 
WHERE 
    t1.link=12 
    AND t1.status=1 
GROUP BY 
    t2.created_by,
    t3.created_by

You also don't normally want to group by fields you aren't naming in your select clause: 您通常也不想按未在select子句中命名的字段分组:

SELECT 
    t1.name, 
    SUM(t2.total_amount+t3.total_amount) 
FROM 
    userdetails t1 
        LEFT JOIN savings t2 
            ON t2.created_by=t1.id 
            AND t2.status=2 
        LEFT JOIN expense t3 
            ON t3.created_by=t1.id 
            AND t3.status=2 
WHERE 
    t1.link=12 
    AND t1.status=1 
GROUP BY 
    t1.name
select 
    u.Name,
    u.link,
    c.total
from user_details u,
(select 
    "created by",
    sum(amount) Total
from 
    (select 
        "created by",
        amount
    from expense
    union all
    select 
        "created by",
        amount
    from savings)
    group by "created by") c
where u.ID=c."created by"
and  u.link=12;

Try this: 尝试这个:

SELECT A.Name,A.link,B.total
FROM user_details A,
  (SELECT 'created by', SUM(amount) AS Total
  FROM
    (SELECT 'created by',amount
    FROM expense
    UNION ALL
    SELECT'created by',amount
    FROM savings)
  GROUP BY 'created by') B
WHERE A.ID=B.'created by'
AND A.link=12;

You can use field_name as 'Field Name' or 'Column Title' to display it in the results. 您可以将field_name用作'Field Name''Column Title'以将其显示在结果中。 Make sure you have a column created by in the table. 确保您在表中created by一个列。

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

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