简体   繁体   English

MySql中的分组和最大数目

[英]The grouping and the largest number in MySql

Good afternoon! 下午好! I started studying MySql and faced with a problem: 我开始学习MySql并遇到问题:

Query Error: Error: ER_WRONG_FIELD_WITH_GROUP: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.product.name' which is not functionally dependent on columns in GROUP BY clause; 查询错误:错误:ER_WRONG_FIELD_WITH_GROUP:SELECT列表的表达式#1不在GROUP BY子句中,并且包含未聚合的列'test.product.name',该列在功能上不依赖于GROUP BY子句中的列; this is incompatible with sql_mode=only_full_group_by 这与sql_mode = only_full_group_by不兼容

I used this site to study https://www.db-fiddle.com/f/xxV16kmnZKmPktUdMSK6xZ/0 我使用这个网站来研究https://www.db-fiddle.com/f/xxV16kmnZKmPktUdMSK6xZ/0

My task is to make: find a region with the lowest price for each product. 我的任务是: 找到每种产品价格最低的地区。 When there are multiple regions with the same price, choose the first one in the alphabetical order. 当多个地区的价格相同时,请按字母顺序选择第一个。

My table: 我的桌子:

create table product(id int, name varchar(99));
create table region(id int, name varchar(99));
create table price(productId int, regionId int, price decimal(9, 2));

insert into product values(1, 'Crab');
insert into product values(2, 'Crayfish');

insert into region values(1, 'Kiev');
insert into region values(2, 'Kharkov');
insert into region values(3, 'Lvov');

insert into price values(1, 1, 100);
insert into price values(1, 2, 100);
insert into price values(1, 3, 200);
insert into price values(2, 1, 200);
insert into price values(2, 2, 100);
insert into price values(2, 3, 100); 

My select : 我的选择:

select product.name 'product', price.price, region.name 'Region name' 
from price
left join product on product.id = price.productId
left join region on region.id = price.regionId
where price.price != 0
group by region.name
having max(price.price)
order by region.name

Expected result: 预期结果:

product region  price
Crab    Kharkov 100
Crayfish    Kharkov 100

To get the lowest price for each products, you can do: 要获得每种产品的最低价格,您可以执行以下操作:

select p.*
from price p
where p.price = (select min(p2.price) from price p2 where p2.product_id = p.product_id);

Additional join s will bring in the names rather than the id s. 其他join将引入名称而不是id

To get one row per product: 要获得每种产品一行:

select p.product_id, p.price, min(r.name)
from price p join
     region r
     on p.regionId = r.id
where p.price = (select min(p2.price) from price p2 where p2.product_id = p.product_id)
group by p.product_id, p.price;

You could use a join on the max price grouped by product and region 您可以对按产品和地区分组的最高价格使用联接

select t.product, price.price, t.region
from price
INNER JOIN ( 
select price.productID, price.regionId, product.name product, 
          max(price.price) max_price, region.name Region 
from price
left join product on product.id = price.productId
left join region on region.id = price.regionId
where price.price != 0
group by product.name , 
    region.name,
    price.productID,
    price.regionId ) t on t.productId = price.productId  
      and t.regionId = price.regionId and t.max_price =  price.price

I'd use something like that: 我会用类似的东西:

select
  p2.name as product,
  (
    select min(r.name)
    from
      price as c2
      inner join region as r on c2.regionid = r.id
    where c2.productId = t.id and c2.price = t.price) as region,
  t.price
from
  product as p2
  inner join (
    select
      p.id,
      min(c.price) as price
    from
      product as p
      left outer join price as c on p.id = c.productId
    group by p.id) as t on p2.id = t.id

the main idea is to get minimal prices for every product (sub-query t) and then find minimal (ie first alphabetically) region for every product/price combination 主要思想是获得每种产品的最低价格(子查询t),然后为每种产品/价格组合找到最小(即第一个字母)区域

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

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