SUM the quantity and Displaying in column - php

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

Related

how to insert a new record in a table that has foreign key attributes in mysql

What is wrong in this statement since customers table has only one record
INSERT INTO CART (Cartid,custid,Pid)
VALUES ('2',SELECT(custid from CUSTOMERS), SELECT (Pid from Products where Pname ='shirts'))
Make sure that the cart table id is primary key and auto_increment. Get the customer id and store in a variable. You can try something similar like this
INSERT INTO CART (Cartid,custid,Pid) VALUES ('', SELECT custid from CUSTOMERS WHERE custid = '$customer_id'),(SELECT Pid FROM Products WHERE Pname = 'shirts' LIMIT 1));
The correct syntax is:
INSERT INTO CART (Cartid, custid, Pid)
VALUES (2,
(SELECT custid FROM CUSTOMERS LIMIT 1),
(SELECT Pid FROM Products WHERE Pname = 'shirts' LIMIT 1)
);
Your parentheses are in the wrong place.
Notes:
I added the LIMIT just to enforce that one row is returned.
I removed the single quotes around '2', because ids are usually number.
You probably should not be inserting the id; it should be an auto-increment column.

mysql query to get count of products along with quantity

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

php shopping cart -group by selection and get the total

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...

copy all rows with matching field value

I would like to copy all mysql rows with a matching field value into a new row and change one field.
So I have a mysql table with the following fields
`lessonplan`, `group`, `category`, `sort_id`, `item`
So I want to copy all rows that have lessonplan=10 and set change the value of lessonplan to 11 for all new rows. Lesson plan is not an auto increment field.
What is best practice for a scenario like this one?
INSERT INTO tableName( lessonplan, `group`, category, sort_id, item )
SELECT 11, `group`, category, sort_id, item
FROM tableName
WHERE lessonplan = 10

updating sql values with array which have a couple times occured items

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.

Categories