dhimok Posted August 31, 2007 Share Posted August 31, 2007 Hello I have two tables 1. products 2. orders How can i select the 5 top sold products from orders? Orders table holds product_id and the quantity thanks Quote Link to comment Share on other sites More sharing options...
isaac_cm Posted September 1, 2007 Share Posted September 1, 2007 something like that select sum(quantity) as Q from products, orders where order.prod_id = products.prod_id group by prod_id order by Q limit 5 Quote Link to comment Share on other sites More sharing options...
dhimok Posted September 1, 2007 Author Share Posted September 1, 2007 I get this error Column 'product_id' in group statement is ambiguous Quote Link to comment Share on other sites More sharing options...
isaac_cm Posted September 1, 2007 Share Posted September 1, 2007 you have to add it to select clause like this select product_id, sum(quantity) as Q from products, orders Quote Link to comment Share on other sites More sharing options...
dhimok Posted September 1, 2007 Author Share Posted September 1, 2007 stil it doesnt change Quote Link to comment Share on other sites More sharing options...
isaac_cm Posted September 1, 2007 Share Posted September 1, 2007 try this products.prod_id Quote Link to comment Share on other sites More sharing options...
fenway Posted September 4, 2007 Share Posted September 4, 2007 Better yet, use a JOIN, and use ORDER BY yourField desc Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.