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
Related
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;
I've got stuck at joining two tables and fetching the min value of a price.
I have two tables "tb1" and "tb2".
Both includes ean, price and some other columns.
I wan't to get the cheapest price for same ean's.
So I got this left join:
SELECT price,* FROM tb1 as w left join tb2 as e on w.ean=e.ean
But how do I get the cheapest price?
I know MIN() but the problem is, the min price could be in tb1 or tb2...
Hope you could help me :)
Regards :)
You can use the following solution using a sub-select with UNION ALL to merge the tables into one (tbAll):
SELECT ean, MIN(price) FROM (
SELECT ean, price FROM tb1
UNION ALL
SELECT ean, price FROM tb2
) tbAll
GROUP BY ean
... or another solution using the LEFT JOIN:
SELECT w.ean, MIN(LEAST(w.price, e.price))
FROM tb1 AS w LEFT JOIN tb2 AS e ON w.ean = e.ean
GROUP BY w.ean
In case you need additional information, you can use the UNION ALL solution with additional ROW_NUMBER (available since MySQL 8.0):
SELECT t.ean, t.price, t.merchant FROM (
SELECT ean, price, merchant, ROW_NUMBER() OVER (PARTITION BY ean ORDER BY ean, price) AS rn
FROM (
SELECT ean, price, merchant FROM tb1
UNION ALL
SELECT ean, price, merchant FROM tb2
) tbAll
)t WHERE t.rn = 1
... or in case you are using a MySQL version earlier 8.0 you can use the following:
SELECT t.ean, t.price, t.merchant FROM (
SELECT ean, price, merchant, IF(#prev <> ean, #rn:=0, #rn), #prev:=ean, #rn:=#rn+1 AS rn
FROM (
SELECT ean, price, merchant FROM tb1
UNION ALL
SELECT ean, price, merchant FROM tb2
ORDER BY ean, price
) tbAll JOIN (SELECT #rn:=0) init_rn JOIN (SELECT #prev:='') init_prev
)t WHERE t.rn = 1
demo on dbfiddle.uk
Use Case
CASE
WHEN w.price < e.price THEN w.price
ELSE e.price
END AS MINPRICE
I have this tables
ORDERS
orders_id
order_article_id
order_invoice_id
order_customer_id
order_qty
ARTICLES
articles_id
article_name
article_qty
article_price
article_amount
CUSTOMERS
customers_id
customer_name
customer_position
customer_office
And SQL Join Table
$sql="SELECT customer_name, article_name, orders_id FROM orders
LEFT JOIN articles ON order_article_id = articles_id
LEFT JOIN customers on order_customer_id = customers_id";
From this query I need to get AMOUNT.
Amount is example
USER: MICHAEL
AMOUNT: ORDER ID = order_article_id + order_qty;
Is it possible to do that in MySQL or i need some addition PHP code?
You can do it in MySQL, using a GROUP BY query and a SUM() aggregate function:
SELECT
c.customer_name,
SUM(a.article_price*a.article_quantity) AS amount
FROM
orders o LEFT JOIN articles a
ON o.order_article_id = a.articles_id
LEFT JOIN customers c
ON o.order_customer_id = c.customers_id
GROUP BY
c.customers_id,
c.customer_name
you might want to obtain the AMOUNT per order, then you need to group by also for the order_id:
GROUP BY
o.orders_id,
c.customers_id,
c.customer_name
I'm not so clear in one of mysql.
i have two tables:
tblcategory
id, name
tblproduct
id, Name, Qty, Price, Category_id
and when i use sql to select all catogories to order by product count:
SELECT c.id,c.name,count(p.id) as product_count
FROM tblcategory as c inner join tblproduct as p on c.id=p.category_id
GROUP BY c.id,c.name
ORDER BY product_count;
The result was that some category which has no product was not appear in my result! how can i get all of them?
You need to use a left outer join:
SELECT c.id,c.name,count(p.id) as product_count
FROM tblcategory as c left outer join tblproduct as p on c.id=p.category_id
GROUP BY c.id,c.name
ORDER BY product_count;
The inner join only keeps records that match in both tables. You want all the product categories, even when there are no matches. The left outer join keeps all records in the first table (tblcategory) along with any matching records in the second table (tblproduct). If there are no products on a category, then you'll get a value of 0 instead of a missing row.
i am seeking help on a query which shows movement of a stock item for a particular itemid.
Table1 Name: saleitems Fields: itemid, saledate, qty
Table2 Name: purchaseitems Fields: itemid, purchasedate, qty
Table3 Name: stocktransfers Fields: itemid, trfdate, from, to, qty
I want to pull out mysql data from all three tables and sort them based in the date fields. Any help is much appreciated.
The expected output should look like below
select * from saleitems,purchaseitems,stocktransfers
where your condition order by saledate,purchasedate,trfdate
you can use order by clause as given select whichever fields you want based on condition or join as per your logic and use order by
Try this:
(select itemid, "SALES" as type, saledate as dt, qty from salesitems)
union
(select itemid, "PURCHASE" as type, purchasedate as dt, qty from purchaseitems)
union
(select itemid, "STOCKTRANSFER" as type, trfdate as dt, qty from stocktransferitems)
order by dt;
I hope, I got your question right.
Hope this helps
select * from salesitems a
inner join purchaseitems b on a.itemid=b.itemid
inner join stocktransfer c on a.item_id=c.item_id
order by a.saledate,b.purchasedate,c.trfdate
SELECT a.itemid,a.saledate,a.qty,b.purchasedate,b.qty,c.trfdate,c.from,c.to,c.qty
FROM saleitems a INNER JOIN purchaseitems b
ON a.itemid = b.itemid
INNER JOIN stocktransfers c
ON a.itemid = b.itemid
WHERE a.itemid = 10
ORDER BY a.saledate , b.purchasedate , c.trfdate