MySQL finding the most expensive in each zip code

前端 未结 4 454
盖世英雄少女心
盖世英雄少女心 2021-01-22 06:36

I have a table called Products with the schema (name, city, state, zip_code price).

And I want to find the most expensive products\' name for a given state\'s each zip_c

4条回答
  •  不要未来只要你来
    2021-01-22 07:21

    SELECT
          t.name, t.city, t.zip_code, t.price
    FROM  
           ( SELECT zip_code
                  , MAX(price) as price 
             FROM products 
             WHERE state = 'NJ' 
             GROUP BY zip_code
           ) AS tm 
        JOIN
            products as t
                ON  tm.zip_code = t.zip_code 
                AND tm.price = t.price
    WHERE 
            t.state = 'NJ' 
    

提交回复
热议问题