【问题标题】:How to display two queries results to one如何将两个查询结果显示为一个
【发布时间】:2017-08-11 09:28:54
【问题描述】:

我有两个 mysql 查询,它们为相同类型的列提供了不同的数据。现在我想将这两个结果合并为一个。以下是我的疑问。

查询-1:

SELECT cl.cand_corp_id as corpId, cl.shortlisted_timestamp as updatedDate, count(1) as activityCount
FROM candidates_log cl
where cl.shortlisted_timestamp > DATE_ADD(current_date(), INTERVAL -30 DAY)
group by cl.cand_corp_id
order by cl.shortlisted_timestamp desc

结果 1:

查询-2:

select j.corp_id as corpId, j.created_at as updatedDate, count(1) as activityCount
from jobs j
where j.created_at > DATE_ADD(current_date(), INTERVAL -30 DAY)
group by j.corp_id
order by activityCount, j.created_at desc limit 5

结果-2;

如何通过这两个结果集的并集得到最终结果。

【问题讨论】:

  • 显示你需要的结果。如果两个结果集中都存在同一对 corpId, updatedDate,您需要对 activityCount 求和还是什么?

标签: mysql sql join merge


【解决方案1】:

您可以尝试 UNION ALL(应该在这两个查询之间)。如果列/数据的类型相同,它应该可以工作。

SELECT cl.cand_corp_id as corpId, cl.shortlisted_timestamp as updatedDate, count(1) as activityCount
FROM candidates_log cl
WHERE cl.shortlisted_timestamp > DATE_ADD(current_date(), INTERVAL -30 DAY)
GROUP BY cl.cand_corp_id
ORDER BY cl.shortlisted_timestamp desc
UNION ALL
SELECT j.corp_id as corpId, j.created_at as updatedDate, count(1) as activityCount
FROM jobs j
WHERE j.created_at > DATE_ADD(current_date(), INTERVAL -30 DAY)
GROUP BY j.corp_id
ORDER BY activityCount, j.created_at desc limit 5;

【讨论】:

    【解决方案2】:

    你可以使用 UNION ALL

    select * from (
    You could use union all  
    SELECT cl.cand_corp_id as corpId, cl.shortlisted_timestamp as updatedDate, count(1) as activityCount
    FROM candidates_log cl
    where cl.shortlisted_timestamp > DATE_ADD(current_date(), INTERVAL -30 DAY)
    group by cl.cand_corp_id
    order by cl.shortlisted_timestamp desc
    ) t1
    
    Union ALL 
    
    select * from ( 
    select j.corp_id as corpId, j.created_at as updatedDate, count(1) as activityCount
    from jobs j
    where j.created_at > DATE_ADD(current_date(), INTERVAL -30 DAY)
    group by j.corp_id
    order by activityCount, j.created_at desc limit 5
    ) t2
    order by activityCount, updatedDate
    

    【讨论】:

      【解决方案3】:
      Use UNION ALL Statement :
      
      ( SELECT cl.cand_corp_id as corpId, cl.shortlisted_timestamp as updatedDate,    
        count(1) as activityCount
        FROM candidates_log cl
        where cl.shortlisted_timestamp > DATE_ADD(current_date(), INTERVAL -30 DAY)
        group by cl.cand_corp_id
        order by cl.shortlisted_timestamp desc
      
      )
      UNION ALL
      (
         select j.corp_id as corpId, j.created_at as updatedDate, count(1) as  
         activityCount
         from jobs j
         where j.created_at > DATE_ADD(current_date(), INTERVAL -30 DAY)
         group by j.corp_id
      ) 
      ORDER BY updatedDate LIMIT 5
      

      【讨论】:

        猜你喜欢
        • 2020-12-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-25
        • 1970-01-01
        • 2021-09-09
        • 1970-01-01
        • 2021-02-24
        相关资源
        最近更新 更多