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
Related
I have two tables: tb_subjoe and tb_deliverydetails
I need to sum Price and Quantity column. at this my code mysql When I do
SELECT scode_id, scode_detail, deli_quote, SUM(deli_dequan) AS QUANTITY, SUM(deli_goodsprice) AS Good price, SUM(deli_wagesprice) AS Wage price
FROM tb_deliverydetails
LEFT JOIN tb_subjob
ON tb_subjob.scode_id = tb_deliverydetails.scode_id
GROUP BY `deli_quote`
it not answer for me
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
I want to order by price. But I am getting the price from two different table and compare it to get the minimum price and print it if price is above 0. It is also possible that price is present in only one database. The rough structure of my code is:
query1 = choose products
while = fetch the products details {
query2 = get the price1, order by price1
query3 = get the price2, order by price2
if statement to get the minimum price so price = minimum price
if (price>0) {
echo product result
}
}
How can I order the result according to the price?
EDIT: Product is already selected, i want to acquire the price of products from two different price table
You can do it in just one query, much more efficiently:
SELECT `a`.*, LEAST(`b`.`price`,`c`.`price`) AS `price`
FROM `products` `a`
JOIN `price1` `b` ON `a`.`id`=`b`.`product_id`
JOIN `price2` `c` on `a`.`id`=`c`.`product_id`
WHERE `price`>0
ORDER BY `price` ASC
Adjust according to your database's tables and columns.
How do I write an sql query to display items from 2 tables based on a selection of a criteria which is from a different table.
Tables I have:
Customer table has columns CustomerID, Name, Address, Tel
CustomerOrder table has columns CustomerID, OrderID, Date, TotalAmount, Status
OrderItem table has columns OrderID, ProductCode, UnitPrice, Qty, TotalPrice
So when a CustomerID is selected, I want the orders to be displayed joining these 3 tables. so as below it should display all the orders that the customer has ever placed.
I tried using the query:
Select CustomerOrder.*, OrderItem.*
From CustomerOrder
INNER JOIN OrderItem Where Customer.CustomerID = $CustomerID
But it's not working. Need help in the query and also in displaying the data properly using php.
Can anyone help?
E.g.
CustomerID:__________
OrderID:__1____ Date:______ TotalAmount:______ Status:_____
ProductCode:__ UnitPrice:___ Qty:_____TotalPrice:__________
ProductCode:___ UnitPrice:______ Qty:_____ TotalPrice:_________
OrderID:___2___ Date:______ TotalAmount:______ Status:_____
ProductCode:__ UnitPrice:___ Qty:_____TotalPrice:__________
ProductCode:___ UnitPrice:______ Qty:_____ TotalPrice:_________
Since you don't need to display Customer information at this step, then you query can be :
SELECT co.OrderID
,co.Date
,co.TotalAmount
,co.Status
,ci.ProductCode
,ci.UnitPrice
,ci.Qty
,ci.TotalPrice
FROM CustomerOrder AS co
INNER JOIN OrderItem AS ci ON (ci.orderID = co.OrderID)
WHERE co.CustomerID = $customer
ORDER BY co.OrderID, ci.ProductCode
As you'd like to no repeat order information in your output, your PHP code should be soemthing like this:
$current_order_id = false;
foreach ($data as $row) {
if ($current_order_id!==$row['OrderID']) {
$current_order_id = $row['OrderID'];
echo "OrderID: ".$row['OrderID']." , Date: ".$row['Date']." , TotalAmount: ".$row['TotalAmount']." , Status: ".$row['Status']." <br>";
}
echo "ProductCode: ".$row['ProductCode']." , UnitPrice: ".$row['UnitPrice']." , Qty: ".$row['Qty']." , TotalPrice: ".$row['TotalPrice']." <br>";
}
Anothere way of doing the same is to first get all the Orders information from CustomerOrder only.
And then loop on the result and get items infotmation OrderItem for each order.
Try like this:
SELECT co.OrderID,date,TotalAmount,ProductCode, UnitPrice,Qty,TotalPrice,Status FROM CustomerOrder AS co
INNER JOIN OrderItem AS oi ON oi.orderID = co.OrderID
INNER JOIN Customer AS c ON c.CustomerID = co.CustomerID
WHERE c.CustomerID = $customer
I have two tables :
- cart , cols are (painting_id, session_id, ip, user_agent)
- paintings , cols (painting_id, price)
Now I have to select the painting id from the table cart. I need to Join the two tables and get the sum of the price of all the paintings from table 'paintings'. Note the table cart doesnt have the price column , it has to be imported from the 'paintings' table. Only the sum of price of those paintings are shown which has been added into the cart table by a particular session id or email id.
Here is the query i have tried so far
SELECT p.SUM(price) FROM paintings
p JOIN cart c ON p.painting_id = c.painting_id
WHERE c.session_id = '$session'
It should be SUM(p.price) instead.
Try this query
SELECT cart.user_agent, sum(paintings.price)
from cart inner join
paintings on
cart.painting_id=paintings.painting_id
where session_id='$session'
if you want the total cost of all the users
SELECT cart.user_agent, sum(paintings.price)
from cart inner join
paintings on
cart.painting_id=paintings.painting_id
group by cart.session_id