2017-03-20 37 views
2

2つのmysqlクエリがあります。これは、同じタイプのカラムに対して異なるデータを与えています。今私はこれらの2つの結果を1つにまとめたいと思っています。以下は私の質問です。2つのクエリ結果を1つに表示するには

クエリ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: enter image description here

クエリ-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。 enter image description here

これら2つの結果セットを結合して最終結果を得る方法を教えてください。

+0

はあなたが必要とする結果を表示します。 'corpId、updatedDate'の同じペアが両方の結果セットに存在する場合、' activityCount'または何を集計する必要がありますか? – Serg

答えて

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 
1

UNION ALL(これら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 
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; 
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 
関連する問題