php - How to group database tables using Group By -
i developing simple php shopping cart site connected databse named products , has tables orders,user,products. orders table has following fields : orderid , product_id , user_id , quantity ... when user orders example 3 products . data inserted session['cart']
below query
$query = "insert orders (product_id,user_id,quantity) values ('$key','$id','$value')"; mysql_query($query);
now problem if user orders more 1 product, 3 orderid's generated, because ordered 3 products , each orderid refers product. when use select statement shows orders of users want group them, if person ordered more 1 products @ time, list show me single orderid products user ordered it. below image of database table record orders table , select query using ..
orderid product_id user_id quantity
1 4 1 1 3 21 1 3 4 19 1 1 5 21 1 3 6 19 1 2 $sql1=mysql_query("select * orders,user user.`user_id`=orders.`user_id`"); while($row = mysql_fetch_array($sql1)) { $sreturn .=' <tr> <td>' . $row['user_id'] . '</td> <td>' . $row['orderid'] . '</td> <td>' . $row['product_id'] . '</td> <td>' . $row['quantity'] . '</td> </tr>'; }
what select query can used told me groupby sum ..
this u sum of products each user according product_id
updated
select sum( orders.quantity ) , count( orders.order_id ) , orders.user_id orders join user on (user.`user_id`=orders.`user_id`) group orders.user_id
Comments
Post a Comment