Update Subtract qty - SQL - php

I have the following DB structure
Table product (which holds my products information including the stock)
prod_id
prod_name
prod_desc
prod_price
stock
table store_orders_item (which is updated every time a customer buys from the my site)
id
order_id
sel_prod_id
sel_prod_price
sel_prod_qty
I need that every time I make a sale online and store_orders_items is updates, that reduce the stock field of the product sold on the table product
I think i need to use the code below, plus some sort of join or subquery to update my product table according to the prod_id, but i could not figure out how.
UPDATE product SET stock = stock - (SELECT SUM(sel_prod_qty) FROM store_orders_items);
I also tried:
UPDATE product
SET stock = stock - (SELECT s.sel_prod_qty, s.sel_prod_id, p.prod_id
FROM store_orders_items as s left join product as p ON
s.sel_prod_id=p.prod_id WHERE order_id=(SELECT MAX(order_id) from orders_number));
And got this error:
"You can't specify target table 'product' for update in FROM clause"

You must use join updates, for example in MySQL:
UPDATE
product p
INNER JOIN
store_orders_item s
ON
p.prod_id = s.sel_prod_id
SET
p.stock = p.stock - s.sel_prod_qty
WHERE
s.order_id = NNNNN
where NNNNN is the id of order just inserted.

Related

Select only those records that have multiple value in same table

I have only those rows that have multiple rows in the same table. For example see below image.
In the above picture you can see 2 highlighted columns one is for useid(u_id) and second is for product id (product_id).
So you can see user id of 7 has multiple product like (78,40,44,45,53) and user id 9 has multiple products like (79,75,79) same like user id 40 has multiple products.
so want out put like if particular user have multiple products then it will display 'multiple product' in product name column instead of display all product name.
above picture display all product but i need to display 'multiple product' message instead of all products if particular user have multiple products
I have used following query but not getting result that i want .
SELECT *
FROM orderlist
WHERE product_id IN (
SELECT product_id
FROM orderlist
GROUP BY product_id
HAVING COUNT(*) > 1
)
You could do the JOINS after aggregation
SELECT *,
CASE WHEN c.Counts > 1 THEN 'Multi' ELSE o.ProductName END ProductName
FROM
(
SELECT product_id, COUNT(*) Counts
FROM orderlist
GROUP BY product_id
)c INNER JOIN orderlist o ON o.product_id = c.product_id

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

How could/should I write this SQL query?

I need to write an SQL query to list a customers prices for our products. We have a standard price list, customer_no = 0, and a customer specific price list, customer_no = XXXX.
I am having trouble understanding how I can get the query to return the customer specific price for a product, if they've been given such, or if not fall back to the standard price.
To get all products and prices on the standard price list
select prices.product_id, products.product_desc, prices.m2
from prices, products
where prices.product_id = products.product_id
and prices.customer_no = 0
order by prices.product_id asc
To get all products and prices that the customer has been specifically quoted for
select prices.product_id, products.product_desc, prices.m2
from prices, products
where prices.product_id = products.product_id
and prices.customer_no = $_SESSION['customer']
order by prices.product_id asc
How can I perform the first query, but if the customer has their own price then replace it with that? Is this even possible?
Thanks in advance.
Steve
Edit: Sorry, missed the third line in both queries in the original post.
You have to join with the prices table twice, once for the list prices and then for the quoted prices. Use a LEFT JOIN for the latter, since some products won't have a quoted price. Then use NVL to default from the quoted price to the list price.
SELECT products.product_id, products.product_desc, NVL(p2.m2, p1.m2)
FROM products
JOIN prices p1 ON p1.product_id = products.product_id
LEFT JOIN prices p2 ON p2.product_id = products.product_id
AND p2.customer_no = $_SESSION['customer']
WHERE p1.customer_no = 0
Try:
select prices.product_id, products.product_desc, prices.m2
from prices, products
where prices.customer_no = nvl($_SESSION['customer'], 0)
order by prices.product_id asc

How should i order by price in this case

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.

joining tables in mySQL to get a particular column

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

Categories