首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >左连接变成隐式连接我如何修复它?

左连接变成隐式连接我如何修复它?
EN

Stack Overflow用户
提问于 2012-07-14 04:27:42
回答 1查看 135关注 0票数 1

这个查询使一个连接而不是左连接,但我不知道它是如何将使用cierre_mensual表的wheres子句抹掉的:

代码语言:javascript
复制
SELECT last_odometro.equipo, IFNULL(SUM(cierre_mensual.recorrido),0) + 
      last_odometro.recorrido AS ultimo_odometro
FROM (
    SELECT *
    FROM `cierre_mensual`
    WHERE mes < '2012-03-01'
    AND `tipo_de_recorrido`
    IN ( 1, 3 )
    GROUP BY `equipo`
    HAVING MAX( mes ) 
) AS last_odometro
LEFT JOIN 
    `cierre_mensual` ON last_odometro.`equipo` = cierre_mensual.`equipo`
WHERE 
        cierre_mensual.mes > last_odometro.mes
    AND cierre_mensual.mes < '2012-03-01'
GROUP BY `last_odometro`.`equipo`

表字段为: id equipo mes combustible_en_tanque recorrido tipo_de_recorrido

1)我要找到具有tipo_de_recorrido为(1,3) 2)的最后mes (日期)的recorrido,并将剩余的recorrido与较大的mes相加(如果存在

代码语言:javascript
复制
equipo  mes     combustible_en_tanque   recorrido   tipo_de_recorrido
7   2011-07-01  4   100     2
7   2011-07-01  4   100     2
7   2011-08-01  4   193900  1
7   2011-09-01  4   194000  1  <- the last row of type 1 194000
7   2011-10-01  4   100     2   +=100
7   2011-11-01  4   100     2   +=100 
7   2011-12-01  4   100     2   +=100
7   2012-01-01  7   150     2   +=150
7   2012-02-01  4   50  2       +=50

它的回报:

代码语言:javascript
复制
equipo  ultimo_odometro
   7     195150  

但是如果:

代码语言:javascript
复制
7   2011-07-01  4   100     2
7   2011-07-01  4   100     2
7   2011-08-01  4   193900  1
7   2011-09-01  4   194000  1  <- the last row of type 1 194000
no more rows whit type!=(1,2)

必须返回194000,但联接失败。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-14 04:33:18

您可以将条件从WHERE子句移动到LEFT JOINON子句:

代码语言:javascript
复制
LEFT
JOIN `cierre_mensual`
  ON last_odometro.`equipo` = cierre_mensual.`equipo`
 AND cierre_mensual.mes > last_odometro.mes
 AND cierre_mensual.mes < '2012-03-01'
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11477907

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档