UNION ALL 有 4 张桌子

Posted

技术标签:

【中文标题】UNION ALL 有 4 张桌子【英文标题】:UNION ALL with 4 tables 【发布时间】:2020-12-30 05:23:12 【问题描述】:

我对 MSSQL 和全栈开发非常陌生,我想用 4 个不同的表执行 UNION ALL 用于贷款储蓄管理系统 下面的代码是我有 4 个表的存储过程,我得到了错误

消息 207,级别 16,状态 1,程序 USP_GetLoanSavingsDetailsByLoanTakerId,第 13 行 列名“GetSavings”无效。

消息 207,级别 16,状态 1,程序 USP_GetLoanSavingsDetailsByLoanTakerId,第 13 行 列名“GiveSavings”无效

这是我的代码,我想实现 Loan Give to a person, Loan Received from a person, 从某人那里获得的储蓄和给予在同一天进行交易的人的储蓄 MemberId 是我要贷款并将储蓄提供给的人,UserId 是希望将贷款提供给会员并获得/提供储蓄给同一个人的人

ALTER PROCEDURE [dbo].[USP_GetLoanSavingsDetailsByLoanTakerId]
(
    @MemberId bigint,
    @UserId bigint
)
AS
BEGIN
    
    --EXEC USP_GetLoanSavingsDetailsByLoanTakerId 5, 26


SELECT EntryDate, SUM(LoanGiven) AS LoanGiven, SUM(LoanReceived) AS LoanReceived , SUM(GetSavings) AS GetSavings, SUM(GiveSavings) AS GiveSavings
FROM(  
    SELECT EntryDate, TotalAmount as LoanGiven, 0 as LoanReceived
    FROM tblGiveLoan 
    WHERE UserId = @UserId and MemberId = @MemberId
    UNION ALL   
    SELECT EntryDate, 0 as LoanGiven, Amount as LoanReceived
    FROM tblReceiveLoan
    WHERE UserId = @UserId and MemberId = @MemberId
    UNION ALL
    SELECT EntryDate, TotalAmount as GetSavings, 0 as GiveSavings
    FROM tblGetSavings 
    WHERE UserId = @UserId and MemberId = @MemberId
    UNION ALL   
    SELECT EntryDate, 0 as GetSavings , TotalAmount as GiveSavings
    FROM tblGiveSavings
    WHERE UserId = @UserId and MemberId = @MemberId
) t
GROUP BY EntryDate
ORDER BY EntryDate ASC  
 END

【问题讨论】:

错误消息声称您使用的是 MS SQL aka SQL Server,而不是 mysql。该问题已重新标记。 我遇到错误在 UNION 中,输出行集的名称取自第一个子查询,所有其他子查询的名称/别名都被忽略。 请确认是MySQL还是SQL Server,并相应更新您的问题(第一句和标签)。 它是 SQL Server @Akina 我还有一个问题你能帮我吗? ***.com/questions/65918586/… 【参考方案1】:
ALTER PROCEDURE [dbo].[USP_GetLoanSavingsDetailsByLoanTakerId]
(
    @MemberId bigint,
    @UserId bigint
)
AS
BEGIN
    
    --EXEC USP_GetLoanSavingsDetailsByLoanTakerId 5, 26


SELECT EntryDate, SUM(LoanGiven) AS LoanGiven, SUM(LoanReceived) AS LoanReceived , SUM(GetSavings) AS GetSavings, SUM(GiveSavings) AS GiveSavings
FROM(  
        SELECT EntryDate, TotalAmount as LoanGiven, 0 as LoanReceived, 0 as GetSavings, 0 as GiveSavings
        FROM tblGiveLoan 
        WHERE UserId = @UserId and MemberId = @MemberId
    UNION ALL   
        SELECT EntryDate, 0, Amount, 0, 0
        FROM tblReceiveLoan
        WHERE UserId = @UserId and MemberId = @MemberId
    UNION ALL
        SELECT EntryDate, 0, 0, TotalAmount, 0
        FROM tblGetSavings 
        WHERE UserId = @UserId and MemberId = @MemberId
    UNION ALL   
        SELECT EntryDate, 0, 0, 0, TotalAmount
        FROM tblGiveSavings
        WHERE UserId = @UserId and MemberId = @MemberId
    ) t
GROUP BY EntryDate
ORDER BY EntryDate ASC  
END

【讨论】:

谢谢它工作得很好非常感谢

以上是关于UNION ALL 有 4 张桌子的主要内容,如果未能解决你的问题,请参考以下文章

SQL-Union ALL 和 except

在 MySQL 中使用 Union All 和 Order By

按日期排序 UNION ALL 上的选择查询

Union all 似乎没有按预期工作。返回更少的行

Mysql查询与Union All和Join for League表

替换 Union All 加入以提高性能