I need to ask a question in mysql where I have two sales and purchase tables these have the date field and I want to take c.fechacompra < v.fechaventa The problem is that I have many purchase date less than the date-of-sale and the query gets the lowest of all and not the least immediate, that is, the most recent date among the previous ones. date sale .