数据库 Mysql 多表查询,left join联合两个sql示例

SELECT 
    t1.RowID,
    t1.UserID,
    t1.CreateDate,
    t1.BatchState,
    t2.InputDataCount,
    t1.Qty
FROM (
    SELECT 
        @curRow := @curRow + 1 AS RowID,
        `UserID`,
        DATE_FORMAT(CreateDate, '%Y-%m-%d') AS CreateDate,
        BatchState,
        SUM(`InputDataCount`) AS InputDataCount,
        SUM(`ProccessedPages`) AS Qty
    FROM 
        `be_UserDoBatchRecord`,(SELECT @curRow := 0) r	            
    WHERE 
        1=1  AND  DATE_FORMAT(CreateDate,'%Y-%m-%d') >= '2023-10-01' AND  DATE_FORMAT(CreateDate,'%Y-%m-%d') <= '2023-10-16' GROUP BY 
		            UserID,
		            DATE_FORMAT(CreateDate,'%Y-%m-%d'),
		            BatchState) AS t1
LEFT JOIN (
    SELECT IX_ZLRQ AS CreateDate, IX_ZLR AS UserID, COUNT(IX_ZLR) AS InputDataCount
    FROM (
        SELECT IX_ZLR, IX_ZLRQ FROM ix_jnml01 
        WHERE 1=1 AND  DATE_FORMAT(IX_ZLRQ, '%Y-%m-%d') >= '2023-10-01' AND  DATE_FORMAT(IX_ZLRQ, '%Y-%m-%d') <= '2023-10-16'
        
        UNION ALL
        SELECT IX_ZLR, IX_ZLRQ FROM ix_jnml02
        WHERE 1=1 AND  DATE_FORMAT(IX_ZLRQ, '%Y-%m-%d') >= '2023-10-01' AND  DATE_FORMAT(IX_ZLRQ, '%Y-%m-%d') <= '2023-10-16'
        
        UNION ALL
        SELECT IX_ZLR, IX_ZLRQ FROM ix_jnml03
        WHERE 1=1 AND  DATE_FORMAT(IX_ZLRQ, '%Y-%m-%d') >= '2023-10-01' AND  DATE_FORMAT(IX_ZLRQ, '%Y-%m-%d') <= '2023-10-16'
        
        UNION ALL
        SELECT IX_ZLR, IX_ZLRQ FROM ix_jnml04
        WHERE 1=1 AND  DATE_FORMAT(IX_ZLRQ, '%Y-%m-%d') >= '2023-10-01' AND  DATE_FORMAT(IX_ZLRQ, '%Y-%m-%d') <= '2023-10-16'
        
        UNION ALL
        SELECT IX_ZLR, IX_ZLRQ FROM ix_jnml05
        WHERE 1=1 AND  DATE_FORMAT(IX_ZLRQ, '%Y-%m-%d') >= '2023-10-01' AND  DATE_FORMAT(IX_ZLRQ, '%Y-%m-%d') <= '2023-10-16'
        
        UNION ALL
        SELECT IX_ZLR, IX_ZLRQ FROM ix_jnml06
        WHERE 1=1 AND  DATE_FORMAT(IX_ZLRQ, '%Y-%m-%d') >= '2023-10-01' AND  DATE_FORMAT(IX_ZLRQ, '%Y-%m-%d') <= '2023-10-16'
    
        UNION ALL
        SELECT IX_ZLR, IX_ZLRQ FROM ix_ywyj01
        WHERE 1=1 AND  DATE_FORMAT(IX_ZLRQ, '%Y-%m-%d') >= '2023-10-01' AND  DATE_FORMAT(IX_ZLRQ, '%Y-%m-%d') <= '2023-10-16'
    
        UNION ALL
        SELECT IX_ZLR, IX_ZLRQ FROM ix_ywyj02
        WHERE 1=1 AND  DATE_FORMAT(IX_ZLRQ, '%Y-%m-%d') >= '2023-10-01' AND  DATE_FORMAT(IX_ZLRQ, '%Y-%m-%d') <= '2023-10-16'
    
        UNION ALL
        SELECT IX_ZLR, IX_ZLRQ FROM ix_ywyj03
        WHERE 1=1 AND  DATE_FORMAT(IX_ZLRQ, '%Y-%m-%d') >= '2023-10-01' AND  DATE_FORMAT(IX_ZLRQ, '%Y-%m-%d') <= '2023-10-16'
    
        UNION ALL
        SELECT IX_ZLR, IX_ZLRQ FROM ix_ywyj04
        WHERE 1=1 AND  DATE_FORMAT(IX_ZLRQ, '%Y-%m-%d') >= '2023-10-01' AND  DATE_FORMAT(IX_ZLRQ, '%Y-%m-%d') <= '2023-10-16'
    
        UNION ALL
        SELECT IX_ZLR, IX_ZLRQ FROM ix_ywyj05
        WHERE 1=1 AND  DATE_FORMAT(IX_ZLRQ, '%Y-%m-%d') >= '2023-10-01' AND  DATE_FORMAT(IX_ZLRQ, '%Y-%m-%d') <= '2023-10-16'
    
        UNION ALL
        SELECT IX_ZLR, IX_ZLRQ FROM ix_ywyj06
        WHERE 1=1 AND  DATE_FORMAT(IX_ZLRQ, '%Y-%m-%d') >= '2023-10-01' AND  DATE_FORMAT(IX_ZLRQ, '%Y-%m-%d') <= '2023-10-16'
    ) AS CombinedTables
    GROUP BY IX_ZLRQ, IX_ZLR
) AS t2
ON t1.CreateDate = t2.CreateDate AND t1.UserID = t2.UserID;

这个 SQL 查询语句是一个复杂的查询,它从多个表中检索数据,并使用 LEFT JOIN 连接这些表。以下是对该查询的分析:

查询主要目的: 这个查询的目的是检索出满足一定日期范围条件的数据,并将结果返回。查询涉及到了几个表,包括 be_UserDoBatchRecord、ix_jnml01、ix_jnml02 等。

子查询 t1: 第一个子查询 (t1) 从 be_UserDoBatchRecord 表中选取数据。其中,使用 DATE_FORMAT 函数将 CreateDate 字段转换成指定格式的日期,并将结果作为 CreateDate 列的值。然后,使用 GROUP BY 按照 UserID、CreateDate 和 BatchState 进行分组,计算出每个分组的 InputDataCount(InputDataCount 的计算使用了 SUM 聚合函数)和 Qty(Qty 的计算也使用了 SUM 聚合函数)。

子查询 t2: 第二个子查询 (t2) 是一个 UNION ALL 的组合查询,从多个表中选取数据。每个子查询都使用了相同的日期范围条件,并计算每个日期与用户的匹配数 (InputDataCount),然后按照日期和用户 (IX_ZLRQ 和 IX_ZLR) 进行分组。

LEFT JOIN 连接: 最后,将子查询 t1 和 t2 使用 LEFT JOIN 连接,并通过 t1 的 CreateDate 和 UserID 与 t2 的 CreateDate 和 UserID 进行匹配。匹配成功的记录将包含在查询结果中,并返回包含 t1 和 t2 列的结果集。

你可能感兴趣的:(数据库,数据库,mysql,sql)