Forum Moderators: coopster

Message Too Old, No Replies

MySQL - select question

         

joedub

9:45 am on May 26, 2005 (gmt 0)

10+ Year Member



Hello,

Ive been fiddling around with a mysql query but cant get the result i want.

Say i have the following table "orders":

OrderID OrderName OrderQty
1 apple 4
2 oranges 2
3 grapes 2
4 peaches 2
5 apple 6
6 peaches 3
7 bananna 2

I want to return the data like so:

apple 10
peaches 5
bananna 2
grapes 2
oranges 2

Any help greatly appreciated

cheers

Span

10:15 am on May 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello,

I think you mean this:

SELECT OrderName,OrderQty FROM orders ORDER BY OrderQty DESC;

joedub

10:22 am on May 26, 2005 (gmt 0)

10+ Year Member



Yes, but i want the data entered into OrderName to be merged.

"SELECT OrderName,OrderQty FROM orders ORDER BY OrderQty DESC;"

will produce the result:
apple 6
apple 4
peaches 3
bananna 2
grapes 2
oranges 2
peaches 2

whereas im tyring to combine identical entries to get:
apple 10
peaches 5
bananna 2
grapes 2
oranges 2

cheers

Span

10:52 am on May 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Does this work:

SELECT OrderName,OrderQty,count(*) FROM orders GROUP BY OrderQty ORDER BY 3 DESC;

where 3 is the number of the column that gets ordered

arran

11:16 am on May 26, 2005 (gmt 0)

10+ Year Member



Looks like you want sum not count:

select OrderName, sum(OrderQty) as 'total' from orders group by OrderName order by total desc;

joedub

11:42 am on May 26, 2005 (gmt 0)

10+ Year Member



Thanks, exactly what i was looking for!