Mysql Merge 2查询

时间:2014-03-06 06:49:42

标签: mysql

我正在尝试在单行中合并下面的查询,以便结果出现在同一行中。 company_id在两个查询中都很常见。 需要建议???

第一次查询:

select c.id as company_id,
    AVG(DATEDIFF(cdr.created_at,cd.send_to_brand_date)) 
        as Avg_time_taken_for_first_comment 
from 
    complaint3 c3,
    complaint3_details cd,
    complaint3_diary cdr,
    company c
where c3.id=cd.complaint_id
    and cd.send_to_brand=1
    and c.id=c3.company_id
    and cdr.complaint_id=c3.id
    and cdr.id in(
        select min(id) 
        from complaint3_diary 
        where complaint_id=c3.id 
            and user_id IN(
                select id from user3 
                where user_type=2))
    and c.id=351
    and c3.created_at>='2014-01-01'
    and c3.created_at<='2014-01-30'
    and cdr.created_at>='2014-01-01'
    and cdr.created_at<='2014-01-30'

第二次质疑:

select c.id as company_id,
    avg(datediff(cs.created_at,cd.send_to_brand_date))  
        as avg_time_closure
from complaint3 c3,
    complaint3_status cs,
    complaint3_details cd,
    company c
where c3.id=cs.complaint_id
    and c3.id=cd.complaint_id
    and c.id=c3.company_id
    and cs.complaint_id=c3.id
    and cd.send_to_brand=1
    and cs.id IN(
        select max(id) 
        from complaint3_status 
        where complaint_id=c3.id 
            and status_type IN(2)
    )
    and cs.status_value=2
    and c.id=272
    and cs.created_at>='2014-01-01'
    and cs.created_at<='2014-01-30'

2 个答案:

答案 0 :(得分:0)

SELECT *
FROM (<1st query>) AS q1
JOIN (<2nd query>) AS q2
ON q1.company_id = q2.company_id

答案 1 :(得分:0)

您可以将两个Queryes用作子查询
SELECT(第一个查询),(第二个查询)FROM&lt;&lt;任何表&gt;&gt;