K
K
Kirill2018-04-20 21:01:47
MySQL
Kirill, 2018-04-20 21:01:47

How to select max value and return all row data?

Hello!
Guys, I'm breaking my head, I can't make a request to the database, I'm stupid even.
The discount table has 4 fields
id_skidki - int auto increment
company_id - int - ID of the company that provides the discount
percent_discount - tinyint - how much% discount is provided
end_date - DATETIME - until what date the discount is valid
The point is that I need to find the maximum discount for today and return the entire row with data, but it’s better to join it with the company table by company_id in order to pull out all the company data with one query so that there is no heavy load on the database.
I would be grateful for a hint, I'm sitting dumb (((

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2018-04-20
@Lobanov

More or less like this:

SELECT * FROM discount
  LEFT JOIN company ON discount.company_id=company.id
  WHERE discount.end_date < CURDATE() + INTERVAL 1 DAY
  ORDER BY percent_discount DESC
  LIMIT 1

(there may be typos, there is no place to check)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question