Problems Linking the max value of an attribute in a table to its primary Key

dba.stackexchange https://dba.stackexchange.com/questions/222494

  •  15-01-2021
  •  | 
  •  

I need to extract the order_id with the max number of items, I've figured out how to extract the max value but I'm having problems linking it to the particular order_id.

PS: I'm a uni student so please go easy on me.

select MAX(f), x.order_id 
from ( select count( distinct z.item_id) f, z.order_id as g 
       from  Order_item z, Item i, Ordert o
       where o.order_id=z.order_id 
       group by z.order_id
      ) AS T, Ordert x
having x.order_id=g;
有帮助吗?

解决方案

Maybe you need simple

select count( distinct z.item_id) f, z.order_id as g 
from  Order_item z, /* Item i, */ Ordert o
where o.order_id=z.order_id 
group by z.order_id
order by f desc limit 1

?

许可以下: CC-BY-SA归因
不隶属于 dba.stackexchange
scroll top