Good afternoon! I started studying MySql and faced with a problem:
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; this is incompatible with sql_mode=only_full_group_by
I used this site to study 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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.