[prog] newbie SQL query assistance needed
Bethany Seeger
seeger at prosensing.com
Tue Apr 27 20:23:52 UTC 2010
Wondering if she wants to include the quantity field at all --
A) ProdX was part of 50 orders or
B) ProdX was part of 50 orders, selling a total of Y ProdX. ]
I think Chris's solution below solves for A)
My MySQL is too rusty to offer a solution.
-Bethany
Chris Wilson wrote:
> On Tue, 27 Apr 2010, Chris Wilson wrote:
>
>> On Tue, 27 Apr 2010, Tina wrote:
>>
>>
>>> I have an orders table. In it are all the orders (orderid) with each item
>>> ordered (itemid) and quantity (quantity).
>>>
>>> I need to figure out which item was ordered the most times. I know it
>>> involves a multi-row query, but I just can't seem to figure it out.
>>>
>> select itemid, count(1) from orders group by itemid;
>>
>
> Actually, sorry, I just realised that you only want the first one. To do
> that, use:
>
> select itemid from orders group by itemid order by count(1) desc
> limit 1;
>
> Cheers, Chris.
>
More information about the Programming
mailing list