选择2个条件不同的列

时间:2019-05-10 05:46:16

标签: mysql join derived-table

我需要创建一个查询,该查询为我按周分组的2019年完成的未付款交易。我已经创建了查询来生成已完成的交易,但是在尝试将未付费交易查询合并到已完成的交易查询中时遇到了麻烦

这是完成交易的查询

SELECT WEEK(A.plat_create_time, 1) AS 'Week Create Time',
       COUNT(t1.lp_sign_time) AS 'Completed Order'
FROM deli_order A
LEFT JOIN
(
    SELECT order_code, code, lp_sign_time
    FROM pg_send_package 
    UNION
    SELECT D.order_code, D.oms_code, C.cm_sign_time
    FROM pg_package C
    INNER JOIN pg_order D ON C.pg_order_id = D.id
) t1 ON t1.order_code = A.order_code
        AND t1.code = A.code
        AND YEAR(A.plat_create_time) = 2019
WHERE (YEAR(A.plat_create_time) = 2019) AND A.status = 6 AND t1.lp_sign_time IS NOT NULL
GROUP BY WEEK(A.plat_create_time, 1);   

它会生成类似这样的内容

Week | Completed Order
1         886
2         734
3         868
4         1000

这是我已经尝试生成已完成和未付款交易的查询

SELECT WEEK(A.plat_create_time, 1) AS 'Week Create Time',
       COUNT(t1.lp_sign_time) AS 'Completed Order',
       COUNT(t2.plat_create_time) AS 'Unpaid Order'
FROM deli_order A
LEFT JOIN
(
    SELECT order_code, code, lp_sign_time
    FROM pg_send_package 
    UNION
    SELECT D.order_code, D.oms_code, C.cm_sign_time
    FROM pg_package C
    INNER JOIN pg_order D ON C.pg_order_id = D.id
) t1 ON t1.order_code = A.order_code
        AND t1.code = A.code
        AND YEAR(A.plat_create_time) = 2019
LEFT JOIN
(   
    SELECT order_code, WEEK(plat_create_time,1) AS 'Create Time'
    FROM deli_order
    WHERE pay_state = 0 AND (YEAR(plat_create_time) = 2019)
) t2 ON t2.order_code = A.order_code
WHERE (YEAR(A.plat_create_time) = 2019) AND A.status = 6 AND t1.lp_sign_time IS NOT NULL
GROUP BY WEEK(A.plat_create_time, 1);

但是当我执行它时,MySQL总是给出错误消息“字段列表中的未知列't2.plat_create_time'。我的预期结果是这样的

Week | Completed Order | Unpaid Order
1         886              51
2         734              42
3         868              40
4         1000             31

查询中应该更改什么?

1 个答案:

答案 0 :(得分:0)

更改:

SELECT order_code, WEEK(plat_create_time,1) AS 'Create Time'

收件人:

SELECT order_code, WEEK(plat_create_time,1) AS 'Create Time', plat_create_time