My question is a little bit long...
This is the is the first picture for my question:
---Here it is the query how I make the selection:
$costumer_name=$_SESSION['p_user'];
$date=date("y-m-d");
$query_8="select * from orders
where costumer_name='$costumer_name' && date='$date'
order by product_id desc";
$result_8=mysql_query($query_8);
---As you see, the selection table needs a group by query and here is that query and the result:
$costumer_name=$_SESSION['p_user'];
$date=date("y-m-d");
$query_8="select * from orders
where costumer_name='$costumer_name' && date='$date'
group by product_id desc";
$result_8=mysql_query($query_8);
---Here it is how I get the SUM:
$query_9="select SUM(totali) as totali from orders where costumer_name='$costumer_name' && date='$date'";
$result_9=mysql_query($query_9);
$row_9=mysql_fetch_array($result_9);
$totali=$row_9['totali'];
---And finaly the problem:
1.How to count the number of orders with the same product_id(sasia)?
2.Get the first total of -cmimi and -sasia?
3.Get the final total?
Thank you
first of all i think there is better solution then every time when somebody click add to cart button to always insert new row in the database. If product is already there you should update quantity by +1, that way you will have only one row of one products instead of 3. I don't know exactly how your database look but here is an idea how you could do that:
CREATE PROCEDURE add_to_cart(IN in_cart_id INT, IN in_product_id...)
BEGIN
DECLARE quant INT;
SELECT quantity
FROM cart
WHERE cart_id = in_cart_id
AND product_id = in_product_id
INTO quant;
IF quant IS NULL then
INSERT INTO cart....
ELSE
UPDATE cart
SET quantity = quantity + 1
WHERE cart_id = in_cart_id
AND product_id = in_product_id;
END IF;
END$$
I think that is much better solution but if you want to do that the way you started then here is a way to do that:
SELECT productId, name, price, COUNT(quantity) as quantity, (price * COUNT(quantity)) as total
FROM where-ever-you store data
WHERE expression
GROUP BY productId;
that is answer on two your question, for the third total sum you need to write separate SELECT statement
SELECT SUM(price * quantity) as total_sum
FROM where-ever-you-store-data
WHERE expression if needed...
Related
My aim is to get values from table products to table sales where sales.Product= products.Productname. This works well. with the code below .
UPDATE sales
SET Amount=(SELECT Selling_Price FROM products WHERE sales.Product= products.Productname);
;
My challenge now is how to update only where sales.Amount= 0. While running this code. I want Column Amount with values ' != 0 ignored .
[...] how to update only where sales.Amount= 0. I want column Amount with values != 0 ignored .
Just add a where clause:
UPDATE sales
SET Amount = (SELECT Selling_Price FROM products WHERE sales.Product = products.Productname)
WHERE Amount <> 0;
If you only want to update rows where amount = 0, then you would add where clause to the update:
UPDATE sales
SET Amount = (SELECT p.Selling_Price
FROM products p
WHERE sales.Product = p.Productname
)
WHERE Amount = 0;
If Amount could be NULL and you want those rows updated as well, you would as OR Amount IS NULL.
SELECT product,quantity,count(product) AS count
FROM order_table WHERE order_id in
(select order_id from progress)
group by product
using this query i get product name and count of products. but, i also have quantity column in my table. so, when product gets 2 quantity how to add that quantity along with product count with this above query.
in the above image there are 3 burger products and 2 quantity for one burger product, here i need to show like product : burger and total quantity : 4 later in next line product : pizza and total quantity : 3
SELECT product,sum(quantity)as 'quantity',count(product) AS count
FROM order_table WHERE order_id in
(select order_id from progress)
group by product
Just a use aggrigate function sum on quantity.
Need some sample data. Input data and expected output.
Till then try it out this query --
SELECT Product
,SUM(Quantity) AS Total_Quantity
,COUNT(Product) AS Product_Count
FROM order_table
WHERE order_id IN (
SELECT DISTINCT order_id
FROM Progress
)
GROUP BY Product
Without sample data it is difficult to guess, but looking at your question, looks like you need something like this
SELECT product
,sum(quantity) as total_quantity
,count(*) AS count
FROM order_table
WHERE order_id in
(select order_id from progress)
group by product
What i want is, when i add amount in "ADD QUANTITY" it will add that quantity in quantity column and shows the updated quantity in quantity text area. eg if i add 25 in add quantity it display 50 in QUANTITY.
VIEW contain Product id,Name, ,Quantity,Price, Add quantity, Update button
and table contains:
p_id,p_name,p_quantity,p_price
What will be the query? as i am using
SELECT SUM(p_quantity)
FROM
(SELECT sum(ADD quantity) AS Quantity
FROM main_inventory
WHERE name='p_quantity')a
but it is not displaying any good result..
Thanks in advance for responce.
SELECT SUM(p_quantity + quantity) as quantity
FROM main_inventory
WHERE name='p_quantity'
Where quantity being set as valid number by php codes.
eg:
SELECT SUM(p_quantity + 25) as quantity
FROM main_inventory
WHERE name='p_quantity'
i think what you want is to display the quantity number to your application before update, query below can do it, however, use programming to handle will be better
DECLARE #SAMPLE TABLE
(
p_id INT IDENTITY(1,1),
p_NAME NVARCHAR(255),
p_Quantity INT,
p_Price INT
)
INSERT INTO #SAMPLE VALUES ('butter',25,200)
INSERT INTO #SAMPLE VALUES ('Cream',300,250)
--YOUR DATABASE ITEM(s)
SELECT * FROM #SAMPLE
--DECLARE USER INPUT(S)
DECLARE #AddQuantity INT
--PLAY AROUND YOUR QUANTITY HERE
SET #AddQuantity = 25
SELECT p_Quantity + #AddQuantity 'Front End Display Output', p_Quantity
'Value in database' FROM #SAMPLE
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.
While i am coding a shopping site, I need to update product stock.
But the thing is, naturally shopping cart can have the same items a couple of times.
What is the best way for updating it?
I tried IN but the following SQL query returns 3 items.
SELECT *
FROM `products`
WHERE id
IN ( 3, 4, 4, 6 )
LIMIT 0 , 30
Here is my solution but, I don't think this is the best one.
$cart = array(1,3,4,4,5,8,22,22);
$itemlist = array_count_values($cart);
foreach($itemlist as $itemid=>$ocurrence){
$SQL = "UPDATE products SET stock = stock-".$ocurrence." WHERE id = ".$itemid;
mysql_query($SQL);
}
You can do something like this:
SELECT * FROM menu WHERE item_id = 1
UNION ALL
SELECT * FROM menu WHERE item_id = 1
UNION ALL
SELECT * FROM menu WHERE item_id = 2
Check this link:
MySQL table -> Can you return the same row multiple times, in the same query?
if possible create a seperate item_cart, table which will have (cart_id, item_id , product_id) as primary key. from here you can get do a group by on product_id to see how many no of product sold.
select product_id, count(product_id) as "No of Product Sold" from item_cart
group by product_id
your php code will update no of products in stock coloumn perfectly.
If possible you try setting triggers for updatin stock columns whenever any product is sold.