联盟两个不同的查询

时间:2017-01-21 17:34:06

标签: mysql

我想在一个表中获得两个查询的结果。这有可能结合这个:

SELECT 
    order_customFields.order_customFields_delivery_method,
    sum(case `order`.order_status when 'paid' then 1 else 0 end) paid,
    sum(case `order`.order_status when 'later' then 1 else 0 end) later,
    sum(case `order`.order_status when 'delivery-approved' then 1 else 0 end) deliveryapproved
FROM 
    order_customFields 
INNER JOIN 
    `order` ON order_customFields.order_id = `order`.order_id
WHERE
    order_customFields.order_customFields_order_date >= date_sub(date_sub(curdate(), interval day(curdate()) - 1 day), interval 1 month) 
    AND order_customFields.order_customFields_order_date < date_sub(curdate(), interval day(curdate()) - 1 day) 
    AND order_customFields.order_customFields_delivery_method IS NOT NULL
GROUP BY    
    order_customFields.order_customFields_delivery_method

用这个

SELECT 
    order_customFields.order_customFields_delivery_method,
    sum(round(`order`.order_totalSumm,2))
FROM 
    order_customFields 
INNER JOIN 
    `order` ON order_customFields.order_id = `order`.order_id
WHERE
    order_customFields.order_customFields_order_date >= date_sub(date_sub(curdate(), interval day(curdate()) - 1 day), interval 1 month)  
    AND order_customFields.order_customFields_order_date < date_sub(curdate(), interval day(curdate()) - 1 day)
    AND order_status = 'paid' 
     OR order_status = 'later' 
     OR order_status = 'delivery-approved'
    AND order_customFields.order_customFields_delivery_method IS NOT NULL
GROUP BY     
    order_customFields.order_customFields_delivery_method`

有不同的行数。

result

2 个答案:

答案 0 :(得分:1)

一旦您拥有相同数量的列和匹配数据类型,就可以使用union,例如:

SELECT 
  order_customFields.order_customFields_delivery_method,
  sum(case `order`.order_status when 'paid' then 1 else 0 end) paid,
  sum(case `order`.order_status when 'later' then 1 else 0 end) later,
  sum(case `order`.order_status when 'delivery-approved' then 1 else 0 end) deliveryapproved
FROM order_customFields 
  INNER JOIN `order` ON order_customFields.order_id = `order`.order_id
WHERE
  order_customFields.order_customFields_order_date >= date_sub(date_sub(curdate(), interval day(curdate()) - 1 day), interval 1 month) and
      order_customFields.order_customFields_order_date < date_sub(curdate(), interval day(curdate()) - 1 day) 
AND order_customFields.order_customFields_delivery_method is not null
GROUP BY    
   order_customFields.order_customFields_delivery_method

UNION  

SELECT 
  order_customFields.order_customFields_delivery_method,
  sum(round(`order`.order_totalSumm,2)),
  null,
  null 
FROM order_customFields 
  INNER JOIN `order` ON order_customFields.order_id = `order`.order_id
WHERE
  order_customFields.order_customFields_order_date >= date_sub(date_sub(curdate(), interval day(curdate()) - 1 day), interval 1 month) and
      order_customFields.order_customFields_order_date < date_sub(curdate(), interval day(curdate()) - 1 day)
and order_status = 'paid' or order_status = 'later' or order_status = 'delivery-approved'
AND order_customFields.order_customFields_delivery_method is not null
GROUP BY    
   order_customFields.order_customFields_delivery_method

,否则 如果您希望结果并排连接,则可以使用连接

select t1.method, t1.paid, t1.later, t1.deliveryapproved, t2.total from (
  SELECT 
  order_customFields.order_customFields_delivery_method method,
  sum(case `order`.order_status when 'paid' then 1 else 0 end) paid,
  sum(case `order`.order_status when 'later' then 1 else 0 end) later,
  sum(case `order`.order_status when 'delivery-approved' then 1 else 0 end) deliveryapproved
FROM order_customFields 
  INNER JOIN `order` ON order_customFields.order_id = `order`.order_id
WHERE
  order_customFields.order_customFields_order_date >= date_sub(date_sub(curdate(), interval day(curdate()) - 1 day), interval 1 month) and
      order_customFields.order_customFields_order_date < date_sub(curdate(), interval day(curdate()) - 1 day) 
AND order_customFields.order_customFields_delivery_method is not null
GROUP BY    
   order_customFields.order_customFields_delivery_method ) t1
left  join ( 
SELECT 
  order_customFields.order_customFields_delivery_method as method ,
  sum(round(`order`.order_totalSumm,2)) total,
  null,
  null 
FROM order_customFields 
  INNER JOIN `order` ON order_customFields.order_id = `order`.order_id
WHERE
  order_customFields.order_customFields_order_date >= date_sub(date_sub(curdate(), interval day(curdate()) - 1 day), interval 1 month) and
      order_customFields.order_customFields_order_date < date_sub(curdate(), interval day(curdate()) - 1 day)
and order_status = 'paid' or order_status = 'later' or order_status = 'delivery-approved'
AND order_customFields.order_customFields_delivery_method is not null
GROUP BY    
   order_customFields.order_customFields_delivery_method ) t2 on t1.method = t2.method

答案 1 :(得分:1)

顺便说一句,第二个查询无法返回所需的结果。我怀疑你想要这个:

SELECT c.order_customFields_delivery_method
     , SUM(ROUND(o.order_totalSumm,2)) rounded
  FROM order_customFields c
  JOIN orders o
    ON o.order_id = c.order_id 
 WHERE c.order_customFields_order_date >= DATE_SUB(DATE_SUB(CURDATE(), INTERVAL DAY(CURDATE()) - 1 DAY), INTERVAL 1 MONTH) 
   AND c.order_customFields_order_date < DATE_SUB(CURDATE(), INTERVAL DAY(CURDATE()) - 1 DAY)
   AND o.order_status IN('paid','later','delivery-approved')
   AND c.order_customFields_delivery_method IS NOT NULL
 GROUP 
    BY c.order_customFields_delivery_method

我已经更改了订单表的名称,因为我无法忍受反击。