简体   繁体   English

三个表之间的内部联接

[英]Inner Join between three tables

I'm using the following query: 我正在使用以下查询:

select a.idclientecrm, max(c.falta) from clientescrmporlistadeclientescrm a
inner join clientescrmporlistadeclientescrm b on (a.idclientecrm=b.idclientecrm and   a.idlistadeclientescrm = 58)
inner join tareas c on a.idclientecrm=c.idclientecrm
where b.idlistadeclientescrm = 70

But I'm not getting the result I want. 但是我没有得到想要的结果。 I know I'm doing something wrong but I don't know what. 我知道我做错了事,但我不知道该怎么办。

I want the outcome of the first inner join (aprox 22k rows) but I need to join the result to the "tareas" table and get the max date from there. 我想要第一次内部联接的结果(大约22,000行),但是我需要将结果联接到“ tareas”表并从那里获取最大日期。

I want to use max because for every idclientecrm , there's more than one row that matches in the "tareas" table and I need the last recorded result. 我想使用max,因为对于每个idclientecrm ,“ tareas”表中有多个匹配的行,并且我需要最后记录的结果。

If I left out something let me know. 如果我遗漏了一些东西,请告诉我。

Thnx in advance! 提前thnx!

You probably want to move the "58" condition to the WHERE clause and group on a.idclientecrm: 您可能希望将“ 58”条件移至WHERE子句并在a.idclientecrm上进行分组:

select a.idclientecrm, max(c.falta) 
from clientescrmporlistadeclientescrm a
inner join clientescrmporlistadeclientescrm b 
on a.idclientecrm = b.idclientecrm
inner join tareas c 
on a.idclientecrm = c.idclientecrm
where b.idlistadeclientescrm = 70
and a.idlistadeclientescrm = 58
group by a.idclientecrm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM