如何将两个查询结果显示为一个

时间:2017-03-20 07:23:12

标签: mysql sql join merge

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

query-1:

sudo make uninstall

结果1: enter image description here

查询-2:

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

结果-2; enter image description here

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

3 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

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

答案 2 :(得分:0)

您可以使用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