Selection of the maximum value from the base and at the same time another field



  • Hello. There's a product base, it has fields. price and price_currency♪ The maximum price is selected from the base MAX(price) How to select the relevant value price_currency the same request?

    UPD: The second request is undesirable because it's a very cumbersome request with the Joines and the big ones.



  • About:

    SELECT other_field, max_field FROM table ORDER BY max_field DESC LIMIT 1
    

    Order the sorting of the field, which shall be the maximum, in reverse. And with help limit You only get one recording (just a claim).
    If the field can be null, you can do it. ORDER BY max_field DESC NULLS LAST

    Telephone applications.
    What do you do when there's not one thing to do?
    The simplest option is to ignore all but one of the first ones:
    But if it's not possible to ignore for any reason, try a request for sub-requests.
    Such as:

    SELECT other_field, max_field
      FROM table t
      JOIN (SELECT MAX(max_field) max FROM table) m ON m.max = t.max_field
    

    Or like,

    SELECT other_field, max_field
      FROM table
     WHERE max_field = (SELECT MAX(max_field) FROM table)
    

    I don't know if there's anything else in place to simplify the task.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2