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 Link to comment https://forums.phpfreaks.com/topic/67511-sql-help/ 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 Link to comment https://forums.phpfreaks.com/topic/67511-sql-help/#findComment-339005 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 Link to comment https://forums.phpfreaks.com/topic/67511-sql-help/#findComment-339026 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 Link to comment https://forums.phpfreaks.com/topic/67511-sql-help/#findComment-339029 Share on other sites More sharing options...
dhimok Posted September 1, 2007 Author Share Posted September 1, 2007 stil it doesnt change Link to comment https://forums.phpfreaks.com/topic/67511-sql-help/#findComment-339031 Share on other sites More sharing options...
isaac_cm Posted September 1, 2007 Share Posted September 1, 2007 try this products.prod_id Link to comment https://forums.phpfreaks.com/topic/67511-sql-help/#findComment-339050 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 Link to comment https://forums.phpfreaks.com/topic/67511-sql-help/#findComment-341532 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.