UPDATE with subquery returning EMPTY/NULL MYSQL - php

I have this database:
table 1 Products: id, name, total_sales
table 2 Products_sales: id_product, id_user, price
I want to update the column "total_sales" using this query
UPDATE products p SET total_sales = (SELECT SUM(price) AS totalPrice
FROM `products_sales` WHERE id_product = p.id GROUP BY id_product)
But if one product doesn't have any sale I get this error:
#1048 - Column 'total_sales' cannot be null
How can I avoid this error when a product has no sales?
Maybe using a fallback value?

As a fallback you may use zero with COALESCE
UPDATE
products AS p
INNER JOIN (
SELECT id_product, COALESCE(SUM(price), 0) AS totalPrice
FROM `products_sales`
GROUP BY id_product
) AS pSub ON p.id = pSub.id_product
SET p.total_sales = pSub.totalPrice;

Related

multiple sun function in a query in multiple tables

I have 3 tables on mySQL database. product, stock_in, stock_out. I want to sum total stock in and stock out of each product by product_id.
I use this query below. But it gives me some false answers.
SELECT product.Product_Name, SUM(stock_in.StockInQuantity) as stockin, S
SUM(stock_out.Quantity) as stockout
FROM product, stock_in, stock_out
WHERE stock_in.Product_ID=product.Product_ID
and stock_out.Product_ID=product.Product_ID
GROUP BY product.Product_ID**
But if I use separate queries for stockin and stockout, results are correct.
SELECT Product_Name, SUM(StockInQuantity) as stockin
FROM product, stock_in
WHERE stock_in.Product_ID=product.Product_ID
GROUP BY product.Product_ID
SELECT Product_Name, SUM(Quantity) as stockout
FROM product, stock_out
WHERE stock_out.Product_ID=product.Product_ID
GROUP BY product.Product_ID**
So please give me a solution to find the result in one query or to echo results of two query results in one table..
You can try below query:
SELECT p.product_name AS Product_Name,
COALESCE(SUM(si.stockin), 0) AS total_stockin,
COALESCE(SUM(so.stockout), 0) AS total_stockout,
FROM product p
LEFT JOIN (SELECT Product_ID, SUM(StockInQuantity) AS stockin
FROM stock_in
GROUP BY Product_ID) si ON si.Product_ID = p.Product_ID
LEFT JOIN (SELECT Product_ID, SUM(Quantity) AS stockout
FROM stock_out
GROUP BY Product_ID) so ON so.Product_ID = p.Product_ID
GROUP BY Product_Name

MySQL update query with where condition only updating one record

I have two tables:
Product details - For storing product information.
Product Listings - For multiple copies that may be sold by different users.
I'm using this select query to gather the lowest price of products with the same name on the listings table = (SELECT product_name, MIN(price) FROM product_listings GROUP BY product_name). $product & $min_price are the variables I'm using for the two fields respectively.
This works fine and gives me the lowest price of all products with the same product_name.
product_name | Lowest price
Title | £0.69
Title | £7.98
Now I want to update the product details table (specifically the "lowest_price" column) with this data so that each dynamically rendered product shows the price of the cheapest copy available.
So far I have managed to update only one record with this update query = ("UPDATE product_details SET lowest_price='$min_price' WHERE product_name='$product'");
Is it possible to update all records with an update query like this? Why is it only updating one record?
Assuming you have a product_id in your product_listings table, which relates to an id field in your product_details table, you can do this:
update products p
inner join (
select product_id, min(price) min_price
from product_listing
group by product_id
) pl
on p.id = pl.product_id
SET p.min_price = pl.min_price
If that's not the case, and you only have the product name to join on (not such a good idea), it'd be this:
update products p
inner join (
select product_name, min(price) min_price
from product_listing
group by product_name
) pl
on p.product_name = pl.product_name
SET p.min_price = pl.min_price
Example fiddle: http://sqlfiddle.com/#!9/9b928/1

data duplicates after adding new fields with the same product_id

I have two tables one called products the other product_images. product_images has 6 fields in this layout.
product_id, small_image, medium_image, large_image, width, height
small_image, medium_image and large_image are null fields.
My data has now duplicated after adding extra fields like small_image and large_image that have the same product_id.
My sql statement
"SELECT *,
i.medium_image, i.width, i.height,
COALESCE((SELECT COUNT(*)
FROM order_details od
WHERE od.product_id = p.product_id), 0) as most_popular
FROM products p
INNER JOIN product_images i on i.product_id = p.product_id
WHERE p.department_id=:department_id AND p.is_active=1
$orderby
LIMIT :limit OFFSET :offset");
What do I need to do change my sql statement to only select the medium_image for the product so it won't duplicate, I tried using DISTINCT but that's not working. I just want to return the single product_image row that is medium_image related to the product_id
Try adding
GROUP BY product_id

Select non registred values in MYSQL

I have two tables in Mysql, Products and Inventory...
Products
int id_product
varchar name
Inventory
id_inventory
product_id
existence
So when my product '3' is out of stock I delete the row in Inventory where product_id = 3
And now I need to get all values that I have deleted on Inventory cause I need to make a report of missing products...
How can I make this query?
I used SELECT i.* FROM inventory i, products p WHERE i.product_id!=p.id_product but it doesn't works..
Thank you
LEFT JOIN would do as you wish as the rows you are looking for would have no corresponding row in inventory for a row in products, and so that gives the query ...
SELECT
p.product_id
FROM product p
LEFT JOIN inventory i
ON i.product_id = p.product_id
WHERE i.product_id IS NULL
Alternatively you could simply just not delete the row from inventory when unavailable. Just then do something like
SELECT
p.product_id
FROM product p
INNER JOIN inventory i
ON i.product_id = p.product_id
AND i.quantityInStock = 0
Either way works
or
select p.* from product p
where not exists (select null from inventory i where i.product_id=p.id_product)

Selectively delete mostly duplicate records from MySQL

I have a table:
PRICE_UPDATE
id (int 5, auto-increment, primary, unique)
part_number (varchar 10, non-null)
price (float(10,2), non-null)
Some of the part_numbers are duplicated (1 or more duplicate records). Sometimes with the same price, sometimes with different prices.
How can I delete all of the duplicate rows based on part_number, leaving either the highest price or just 1 record if the prices were all the same?
Is this even doable in straight MySQL?
DELETE t1
FROM YourTable t1, YourTable t2
WHERE t1.part_number = t2.part_number
AND (t1.price, t1.id) < (t2.price, t2.id)
From inside, to outside:
Selects the ids with the max price per part_number
Selects the max id with the max price per part_number
Deletes the ids not present in 2.
delete tablename where id not in (
(select max(id) from tablename a
inner join
( select id, max(price)
from tablename
group by part_number ) b on a.id = b.id and a.price = b.price
group by part_number))

Categories