Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8222077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:09:12+00:00 2026-06-07T14:09:12+00:00

This query makes one JOIN instead a LEFT join but I don’t know how

  • 0

This query makes one JOIN instead a LEFT join but I don’t know how do it
whitout the wheres clauses that uses cierre_mensual table:

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`

The table fields are:
id equipo mes combustible_en_tanque recorrido tipo_de_recorrido

1)I want to find recorrido whit the last mes(date) that tipo_de_recorrido are (1,3)
2)and sum the rest of the recorrido whith a bigger mes if exist

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

Its returns:

equipo  ultimo_odometro
   7     195150  

but if:

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)

must return 194000 but the join fails.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T14:09:16+00:00Added an answer on June 7, 2026 at 2:09 pm

    You can move conditions from the WHERE clause to the LEFT JOIN‘s ON clause:

    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'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This already involves four INNER JOINs and one LEFT JOIN, but I am not
I created a simple query which contains a JOIN, and this JOIN 'makes' some
I need to make a query to one of Google's services. I read this
I'm currently doing this query to find the guy who makes the most calls:
Could you help me to make this query faster. My query is workable, but
So I need to make 3 tabs with query (got this part down), but
Right now I have one query that returns a list of results and displays
I realise this is quite a common one, but I've been unable to rectify
This is not a problem that needs answering, more of a query on why
Is there any better way to make this query work? I'm looking for a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.