Hi,
i'm implementing a shop.
i want to extract the top 5 items related with the item in input.
the idea is to check inside the bought item to findout the order where the input item appears. When i've these order i count the items and extract the top5.
Code:
CREATE TABLE item(
idItem integer,
name varchar,
description varchar,
price float,
image varchar,
idCategory integer,
stock integer,
foreign key (idCategory) references category(idCategory),
primary key (idItem)
);
CREATE TABLE orders(
idOrder integer,
idCustomer integer,
dateTime timestamp default now(),
foreign key (idCustomer) references customer(idCustomer),
primary key(idOrder)
);
CREATE TABLE bought(
idBought integer,
idOrder integer,
idItem integer ,
quantity integer,
foreign key (idOrder) references orders(idOrder),
foreign key (idItem) references item(idItem),
primary key(idBought)
);
i've done this query:
Code:
select idItem, count(idItem) as count from bought where idOrder in (select idorder from bought where idItem = 1) and idItem<>1 group by iditem order by count DESC
but, how to extract the item? i've the idItem and a counter, how can i have the item table?
there's a better way to write down the same query?