简体   繁体   English

在 mysql 查询中使用 min、max 和 avg

[英]using min, max and avg in mysql query

I have a table Like below.我有一张像下面这样的表。

I want the product_id of Minimum, Maximum and Average cost products in a single query.我想要单个查询中的最小、最大和平均成本产品的 product_id。

CREATE TABLE productlist(product_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
                         cost INT);                             

INSERT INTO productlist(cost)
                 VALUES('2450'),
                       ('2200'),
                       ('2580'),
                       ('2405'),
                       ('3500'),
                       ('1500'),
                       ('1800'),
                       ('1520'),
                       ('1740'),
                       ('1940'),
                       ('2940'),
                       ('1250'),
                       ('1290'),
                       ('1390'),
                       ('2900');

Output:输出:

Min    12
Max    5
Avg    2093

I tried like one below but its not working.我尝试过如下所示,但它不起作用。

SELECT product_id, MIN(cost) as mincost
  FROM productlist
 GROUP BY product_id
 ORDER BY mincost ASC
 LIMIT 0,1
 UNION
SELECT product_id, max(cost) as maxcost
  FROM productlist
 GROUP BY product_id
 ORDER BY maxcost DESC
 LIMIT 0,1

How should I do this我该怎么做

select product_id, cost
from productlist where cost = (SELECT max(cost)from productlist)
union
select product_id, cost
from productlist where cost = (SELECT min(cost)from productlist)
union
select product_id, cost
from productlist where cost = (SELECT x.cost from productlist x, productlist y
GROUP BY x.cost
HAVING SUM(SIGN(1-SIGN(y.cost-x.cost))) = (COUNT(*)+1)/2)

This uses median, returns product id in every case这使用中值,在每种情况下都返回产品 ID

select 'Min', product_id 
from productlist
where cost = (select min(cost) from productlist)
UNION 
select 'Max', product_id
from productlist
where cost = (select MAX(cost) from productlist)
UNION 
select 'Avg', round(AVG(cost),0) as Avg
from productlist

the output you want is not coming by the query you wrote you need to try out this one for getting the required output你想要的输出不是你写的查询你需要尝试这个以获得所需的输出

select 'Min', product_id 
from productlist
where cost = (select min(cost) from productlist)
UNION 
select 'Max', product_id
from productlist
where cost = (select MAX(cost) from productlist)
UNION 
select 'Avg', floor(AVG(cost)) as Avg
from productlist

This is to select all the products这是选择所有产品

  SELECT 
          max(cost), MIN(cost), AVG(cost)
    FROM 
          productlist
    GROUP BY
          product_id

GROUP BY is not exactly required here.这里并不完全需要 GROUP BY。 But seems like you are beginner, googling to will help you.但看起来你是初学者,谷歌搜索会帮助你。

For your question try this.对于你的问题,试试这个。

SELECT 
          (select CONCAT(product_id, '-', cost) from  productlist group by product_id order by cost DESC limit 1) as MAX,
          (select CONCAT(product_id, '-', cost) from  productlist group by product_id order by cost ASC limit 1) as MIN,
          (select avg(cost) from  productlist) as AVG
FROM 
           productlist limit 1

This answers your question exactly but it should be noted that it costs 3 table scans to find this data.这完全回答了您的问题,但应该注意的是,查找此数据需要进行 3 次表扫描。 Also, the example in the question suggests that the average value is truncated down to 2093 from 2093.67.此外,问题中的示例表明平均值从 2093.67 截断到 2093。 It is perhaps better to replace this with round .round替换它也许更好。

SQL Fiddle SQL小提琴

SELECT concat('Min ', product_id) 
  FROM productlist
 WHERE cost = (SELECT min(cost) from productlist)
UNION ALL
SELECT concat('Max ', product_id) 
  FROM productlist
 WHERE cost = (SELECT max(cost) from productlist)
UNION ALL
SELECT concat('Avg ', truncate(avg(cost), 0)) 
  FROM productlist

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM