I am developing a simple php shopping cart site which connected to a databse named products and has tables Orders,user,products. The orders table has the following fields :
OrderId , product_id , user_id , quantity ... When the user orders for example 3 products . The data is inserted from SESSION['cart'] Below is the query
$query = "insert into orders (product_id,user_id,Quantity) values ('$key','$id','$value')";
mysql_query($query);
Now the problem is if user orders more than one product, three orderID's are generated, because he ordered 3 products and each orderId refers to that product. When i use the select statement it shows all the orders of the users but i want to group them, So that if person ordered more than 1 products at a time, the list would show me single orderID but all the products with the user who ordered it. Below is the image of database table record from orders table and the select query i am 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 * FROM orders,USER WHERE 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 be used someone told me groupby with sum ..
this will get u the sum of products by each user according to product_id
Updated
SELECT SUM( orders.Quantity ) , COUNT( orders.order_id ) , orders.user_id
FROM orders join user on (user.`user_id`=orders.`user_id`)
GROUP BY orders.user_id
You need to add a table called something like OrderItem or OrderRow where you put the product information for each order. Fields could be OrderID, ProductID, Quantity, ItemPrice.
It's a good idea to store the price you told the customer at the time of ordering, otherwise you will get a lot of problems when changing prices in the future.
You need to fix your order_id - it cant be autoincrement. Then this is what I think you are asking.
select order_id, sum(quantity)
from orders
group by order_id
I have left the where clause out as I dont know what filter you will want on it.
SELECT * FROM user_table WHERE id =
(SELECT user_id FROM order_table WHERE order_id = n limit 1);
But much better if you will create separate table for order which containing order info and table for linking an order and a products (id, order_id, product_id, quantity).
I have figured out the query .. below is the query for getting number of orders + Quantity or Number OF Products + user_id ...
SELECT user.`user_id`,SUM(quantity) AS Number_Of_Products,COUNT(Orders.OrderID) AS NumberOfOrders FROM Orders
LEFT JOIN USER
ON Orders.user_id=user.`user_id`
GROUP BY username;
below is the output
user_id Number_Of_Products NumberOfOrders
1 10 5
Related
I have the follow table in MySQL
TABLE "SALES"
id, product, code, quantity, amount, who-sold-it
while the field "who-sold-it" is just a example to understand the question but not the real name of the field
Now I have another table the name is "USERS" and it looks like this
id, name, sellercode
I need to get the top 5 sellers using the 2 tables looking the who-sold-it in each SALE and display their name and sold amounts
Order the results by total sales and take the top 5 with limit. You can also join the sales table with the seller table to get the name of the seller.
select users.name, users.sellercode, sum(sales.amount) as total
from sales, users
where sales.sellercode = users.sellercode
group by users.sellercode, users.name
order by total desc
limit 5
To display the result:
<?php while ($row = mysqli_fetch_assoc($result)) ?>
<tr>
<td><?php echo htmlspecialchars($row['name'])</td>
<td><?php echo htmlspecialchars($row['total'])</td>
</tr>
<?php } ?>
I am trying to fetch max column value by grouping other column. I am almost near but stuck at last point. Here is me code
TABLE:
id . sys_id . renewal
1 . aaad00101 . 0
2 . aaad00101 . 1
3 . aaad00104 . 0
4 . aaad00102 . 0
5 . aaad00101 . 2
6 . aaad00103 . 0
SQL Query:
"SELECT * FROM $company WHERE renewal IN (SELECT DISTINCT
MAX(renewal)FROM $company GROUP BY sys_id) ORDER BY sys_id"
Result:
aaad00101-0
aaad00101-2
aaad00102-0
aaad00103-0
aaad00404-0
My code is showing max value with minimum value like top two results. But I want if max value is showing then it should not show minimum value in results.
Let me know what I am missing here.
Thanks
Fixed It:
SQL Query:
"SELECT * FROM $company WHERE id IN (SELECT DISTINCT MAX(id)FROM
$company GROUP BY sys_id) ORDER BY sys_id";
Am I correct that you want to keep all the rows you currently have in your table with an additional column that shows the max renewal value for each sys_id?
It's something frequently done in a window function, but since MySQL doesn't currently have that capability you can get around it with an additional join.
SELECT a.*,
b.last_renewal
FROM company AS a
JOIN ( SELECT sys_id,
MAX( renewal ) AS last_renewal
FROM company
GROUP BY sys_id ) AS b USING ( sys_id ) ;
Or are you looking for a renewal value for the latest occurrence, which I assume is for the max value of the id column?
If that's the case, you can concatenate all of the renewal values into a list and order that list by id, and then you can take out the last value in that list.
SELECT sys_id,
SUBSTRING_INDEX( GROUP_CONCAT( renewal ORDER BY id ),',',-1 ) AS last_renewal,
MAX( id ) AS last_occurance
FROM company
GROUP BY sys_id ;
I have mysql table with Customer id's and post by the customer for various categories.I would like to find out for each category the count of customer post in descending order.
Customer id Category no
1 1
2 1
3 1
1 1
1 2
1 1
2 1
Basically in the table above customer id 1 has highest post for 1 then 2 then 3.
So what mysql query should I use to get the solution .
I really appreciate any help .Thanks in Advance.
This will give you number of post entries in categories table grouped by categories and customers and ordered by their count in desending order:
select customer_id, category_no, count(1) as total_post
from category
group by customer_id, category_no
order by count(1) desc;
SELECT customer_id, COUNT(*) FROM category GROUP BY customer_id, category_no
Group by your 2 columns
To get the highest contributing customer for each category, you can do this:
select category_no, max(cnt) as maxcnt,
substring_index(group_concat(customer_id order by cnt desc)) as cust3
from (select category_no, customer_id, count(*) as cnt
from category
group by category_no, customer_id
) c
group by category_no;
So for my inventory system I have two tables with the same columns names (one for stock produced and one for stock shipped). I figured out how to group the columns by the product and then sum the quantities. So I want to run this query on both tables then subtract the quantity column from each table where the product variables match up.
I use this to add group and sum stock totals (in):
$query = "SELECT id, type, color, product, SUM(Quantity) AS TotalQuantity FROM inventory GROUP BY id, color, type";
And I use this to group and sum stock shipments (out):
$query = "SELECT id, type, color, product, SUM(Quantity) AS TotalQuantity FROM shipped GROUP BY id, color, type";
So how do I subtract the quantity columns for each of these?
Edit:
I use this for output: (a table)
echo '<tr><td>'. $row['product'] . '</td><td id="replace">' . $row['type'] . '</td><td>' . $row['color']. '</td><td>'. $row['TotalQuantity'];
echo "</td></tr>";
This can be done entirely in one query. An INNER JOIN between these will allow you to subtract the quantities. The id, color, product columns are only needed from one of the tables in your SELECT list.
SELECT
inv.id,
inv.color,
inv.product,
/* total inventory quantity */
SUM(inv.Quantity) AS TotalInvQuantity,
/* total shipped quantity */
SUM(ship.Quantity) AS TotalShipQuantity,
/* inventory quantity minus shipped quantity */
SUM(inv.Quantity) - COALESCE(SUM(ship.Quantity), 0) AS SubtractedQuantity
FROM
inventory inv
LEFT JOIN shipped ship ON inv.id = ship.id AND inv.color = ship.color AND inv.product = ship.product
GROUP BY
inv.id,
inv.color,
inv.product
Update after comments
SELECT
inv.id,
inv.color,
inv.product,
inv.TotalInvQuantity,
COALESCE(ship.TotalShipQuantity, 0) AS TotalShipQuantity,
inv.TotalQuantity - COALESCE(ship.TotalQuantity, 0) AS SubtractedQuantity
FROM (
SELECT id, product, color, SUM(Quantity) AS TotalInvQuantity
FROM inventory
GROUP BY id, product, color
) inv
LEFT JOIN (
SELECT id, product, color, SUM(Quantity) AS TotalShipQuantity
FROM inventory
GROUP BY id, product, color
) ship ON
inv.id = ship.id
AND inv.product = ship.product
AND inv.color = ship.color
I have 2 tables in my mySQL database csm and csmproducts.
Table csm stores the customer info and table csmproducts stores the products they ordered.
"Order_ID" is the common variable in both tables.
I have written a query which pulls data from both tables.
My query looks like this:
$query = "SELECT c.*, p.Product_SKU from csm c, csmproducts p where c.Order_ID = p.Order_ID and c.Order_Status='Awaiting Fulfillment' group by Order_ID order by Order_ID DESC LIMIT $startrow, 50";
$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);
}
The trouble I am having is that if one order (hence one Order_ID) has multiple products in it , only the first product is showing.
What do I need to do so that where {$row['Product_SKU']} gives me only the first product for a particular Order_ID, that I am able to pull all the Product_SKUs for that particular Order_ID?
For example Order_ID 5558 has 3 products associated with it (DSC-3433, ASD-6454, UFY-7383)
Currently the output looks like this (showing just the first item the customer orderd)
Order ID: 5558
Product SKU: DSC3433
I would like to see it like this:
Order ID: 5558
Product SKU: DSC-3433, ASD-6454, UFY-7383
That is: all 3 products associated with that Order_ID to be shown.
Thanks in advance for the assistance.
USE GROUP_CONCAT
with exlplanation