LEFT JOIN后的INNER JOIN

时间:2016-01-12 15:30:17

标签: mysql sql join

我有两个效果很好的查询。后者对前者的每一行都运行一次。如果可能的话,我想把它们结合起来。后者返回零行或一行。

问题是两个查询都使用INNER JOIN我需要用LEFT JOIN粘贴它们。这是否合法,并且如果在LEFT JOIN“之后”使用INNER JOIN将会发生什么。最后,这种机制会不会比现有的“快速”方法有任何性能损失?

供参考,如果这是TMI,我很抱歉,这是我的第一个查询:

SELECT
  pmt_attempts.user_id,
  CONCAT(DATE_FORMAT(attempt_time_end, '%Y') + 2, DATE_FORMAT(attempt_time_end, '-%m-%d')) as date_expired,
  course_sku,
  um_fn.meta_value AS 'shipping_first_name',
  um_ln.meta_value AS 'shipping_last_name',
  um_cr.meta_value AS 'credentials',
  pmt_orders.promocode_used,
  pmt_promocodes.sales_channel
FROM
  pmt_attempts
INNER JOIN
  pmt_courses 
    ON pmt_attempts.course_id = pmt_courses.course_id
INNER JOIN
  pmt_orders 
    ON pmt_orders.order_id = pmt_attempts.order_id
INNER JOIN
  wp_usermeta um_fn 
    ON pmt_orders.user_id = um_fn.user_id
    AND um_fn.meta_key = 'shipping_first_name'
INNER JOIN
  wp_usermeta um_ln 
    ON pmt_orders.user_id = um_ln.user_id
    AND um_ln.meta_key = 'shipping_last_name'
LEFT JOIN
  wp_usermeta um_cr 
    ON pmt_orders.user_id = um_cr.user_id
    AND um_cr.meta_key = 'credentials'
INNER JOIN
  pmt_order_course 
    ON fk_order_id = pmt_attempts.order_id
    AND fk_course_id = pmt_attempts.course_id
LEFT JOIN
  pmt_promocodes 
    ON pmt_orders.promocode_used = pmt_promocodes.promocode_text
WHERE
  attempt_time_end >= '2014-01-01' AND <--- CONFIGURABLE REPORT PARAM
  attempt_time_end < '2014-02-01' AND <--- CONFIGURABLE REPORT PARAM
  attempt_complete = 'Y' AND
  (unenroll_date IS NULL OR unenroll_date = '0000-00-00') AND
  1
ORDER BY
  attempt_time_end
;

这是第二个查询:

SELECT
  course_sku,
  pmt_orders.order_date,
  attempt_time_end,
  pmt_order_course.unenroll_date
FROM
  pmt_order_course
INNER JOIN
  pmt_orders ON 
    pmt_orders.order_id = pmt_order_course.fk_order_id
INNER JOIN
  pmt_courses ON 
    pmt_courses.course_id = pmt_order_course.fk_course_id
LEFT JOIN
  pmt_attempts ON 
    pmt_attempts.course_id = pmt_courses.course_id AND
    pmt_attempts.order_id = pmt_orders.order_id AND 
    pmt_attempts.attempt_complete = 'Y'
WHERE
  pmt_orders.user_id = 928 AND <--- FROM FIRST QUERY ROW
  course_sku LIKE 'BL%' AND <--- FIRST TWO LETTERS OF FIRST QUERY ROW + %
  pmt_orders.order_date > '2012-12-14' <--- FROM FIRST QUERY ROW
ORDER BY
  pmt_orders.order_date
LIMIT
  1
;

此查询的目的是为每个订单查找客户是否“续订”并在以后购买另一个相同或类似的产品。

1 个答案:

答案 0 :(得分:1)

在这里使用wloud很棒,但如果你在MySQL FROM subquery就是你需要的。

如果 Q1 ,并且该course_sku列来自pmt_orders表,那么这个长时间的查询,我们可以写:

SELECT
  course_sku,
  pmt_orders.order_date,
  attempt_time_end,
  pmt_order_course.unenroll_date
FROM
    (Q1 here) q1
INNER JOIN pmt_order_course ON (pmt_orders.user_id = q1.user_id AND pmt_orders.course_sku LIKE CONCAT(q1.course_sku,'%') AND pmt_orders.order_date > q1.date_expired)
INNER JOIN pmt_orders ON (pmt_orders.order_id = pmt_order_course.fk_order_id)
INNER JOIN pmt_courses ON (pmt_courses.course_id = pmt_order_course.fk_course_id)
LEFT JOIN pmt_attempts ON (pmt_attempts.course_id = pmt_courses.course_id AND pmt_attempts.order_id = pmt_orders.order_id AND pmt_attempts.attempt_complete = 'Y')
WHERE 1 = 1      
ORDER BY
  pmt_orders.order_date
LIMIT
  1
;