gpt4 book ai didi

MySQL 多个 Left Join 得到错误的输出

转载 作者:行者123 更新时间:2023-11-29 22:09:53 27 4
gpt4 key购买 nike

我是 MySQL 的初学者。我试图从 VoIP 电话卡数据库中查询一些数据,我需要加入多个表。它给出了正确的输出,直到有两个左连接表,但是当我添加第三个表时,“总持续时间”列给出了错误的输出。

我的第一个查询如下所示,输出在此屏幕截图中 enter image description here

select c.login,cname.Name,cname.LastName,DATE_FORMAT(Creation_Date,'%d-%m-%y')as regdate,DATE_FORMAT((Select max(call_start) from calls where calls.id_client = c.id_client),'%d-%m-%y') as lastcall, c.account_state,sum(cdr.duration / 60) as total_duration from clientsshared as c
left join invoiceclients as cname on cname.IdClient = c.id_client
left join calls as cdr on cdr.id_client = c.id_client
where c.id_reseller='10' group by c.id_client order by total_duration desc limit 100

新的查询是这样的,它给出了错误的输出,请检查屏幕截图enter image description here

select c.login,cname.Name,cname.LastName,DATE_FORMAT(Creation_Date,'%m-%d-%y')as regdate, 
(Select max(data) from payments where payments.id_client = c.id_client) as lastpayment,
(Select max(call_start) from calls where calls.id_client = c.id_client) as lastcall,
c.account_state,sum(cdr.duration / 60) as total_duration from clientsshared as c

left join invoiceclients as cname on cname.IdClient = c.id_client
left join payments as p on p.id_client = c.id_client
left join calls as cdr on cdr.id_client = c.id_client
where c.id_reseller='10' group by c.id_client order by total_duration desc limit 100

最佳答案

select c.login,cname.Name,cname.LastName,DATE_FORMAT(Creation_Date,'%m-%d-%y')as regdate, 
(Select max(data) from payments where payments.id_client = c.id_client) as lastpayment,
(Select max(call_start) from calls where calls.id_client = c.id_client) as lastcall,
c.account_state,sum(cdr.duration / 60) as total_duration

from clientsshared as c

left join invoiceclients as cname on cname.IdClient = c.id_client
left join calls as cdr on cdr.id_client = c.id_client
left join payments as p on p.id_client = cdr.id_client

where c.id_reseller='10'

group by c.id_client

order by total_duration desc limit 100

关于MySQL 多个 Left Join 得到错误的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31785707/

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