gpt4 book ai didi

Mysql合并2个查询

转载 作者:行者123 更新时间:2023-11-30 00:35:31 25 4
gpt4 key购买 nike

我正在尝试将以下查询合并到一行中,以便结果出现在同一行中。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'

最佳答案

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

关于Mysql合并2个查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22217170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com