Join two table. One column using SUM - mysql

I need a little help with a MySQL query.
I have two tables one table is a list of product and one table is a list of warehouses quantity
product:
product_id product_name
1 name1
2 name2
3 name3
warehouse_product
id warehouse_id product_id product_quantity
1 1 1 15
2 2 1 30
3 1 2 100
4 2 2 30
5 1 3 20
6 2 3 40
The results Im looking to get from the above data would be
product_id product_name product_quantity
1 name1 45
2 name2 130
3 name3 60
I've tried many query but it's not working. My query is:
SELECT
product_id as product_id, SUM(quantity) as quantity
FROM
(SELECT
p.product_id as product_id, wp.product_quantity as quantity
FROM
product as p
LEFT JOIN warehouse_product as wp ON p.product_id = wp.product_id
WHERE
product_active = 1)

Try this:
SELECT p.product_id, p.product_name, SUM(wp.quantity) as quantity
FROM product as p
LEFT JOIN warehouse_product as wp
ON p.product_id = wp.product_id
AND product_active = 1
GROUP BY p.product_id, p.product_name
After JOINing the two tables you need to use GROUP BY so that SUM of quantity can be calculated for each product.

select p.product_id,p.product_name,sum(product_quantity) as 'product_quantity'
from product p inner join warehouse_product whp on
whp.product_id=p.product_id group by whp.product_id

select p.product_id, p.product_name,sum(w.product_quantity)
from
product p
inner join
warehouse_product w
on
p.product_id = w.product_id
group by w.product_id
order by p.product_name;

SELECT P.productid,
P.ProductName,
Sum(W.product_quantity) As quantity
FROM Product AS P
LEFT JOIN warehouse_product AS W
ON P.productid = W.productid
GROUP BY P.ProductId , P.ProductName
SQLFiddle

Related

MySQL Queries with Meta Keys and Values

I have a hard time wrapping my head around coming up with a nice clean mysql query for this problem. I have two tables:
ORDER ITEMS ORDER ITEM META
----------------- ---------------------
ID Name ID Key Value
----------------- ---------------------
24 Product A 24 _qty 3
30 Product B 30 _qty 5
33 Product B 30 _weight 1000g
55 Product A 33 _qty 1
----------------- 33 _weight 500g
55 _qty 2
---------------------
I ran this query:
SELECT
oi.ID,
oi.Name,
oim1.Value as Qty,
oim2.Value as Weight
FROM
`order_items` as oi
INNER JOIN `order_item_meta` as oim1 ON oim1.ID = oi.ID
INNER JOIN `order_item_meta` as oim2 ON oim2.ID = oi.ID
WHERE
oim1.Key = '_qty' AND
oim2.Key = 'weight'
But it only gives me
-------------------------------
ID Name Qty Weight
-------------------------------
30 Product B 5 1000g
33 Product B 1 500g
-------------------------------
I need to include products that do not have _weight defined as a key so it will give me the following results:
-------------------------------
ID Name Qty Weight
-------------------------------
24 Product A 3
30 Product B 5 1000g
33 Product B 1 500g
55 Product A 2
-------------------------------
Try using an outer join:
select oi.id, oi.name, oim1.value as qty, oim2.value as weight
from order_items as oi
join order_item_meta as oim1
on oim1.id = oi.id
left join order_item_meta as oim2
on oim2.id = oi.id
and oim2.key = '_weight'
where oim1.key = '_qty'
Fiddle Test:
http://sqlfiddle.com/#!2/dd3ad6/2/0
If there is ever a situation where an order doesn't have a quantity you would also have to use an outer join for the quantity, like this:
select oi.id, oi.name, oim1.value as qty, oim2.value as weight
from order_items as oi
left join order_item_meta as oim1
on oim1.id = oi.id
and oim1.key = '_qty'
left join order_item_meta as oim2
on oim2.id = oi.id
and oim2.key = '_weight'
However if an order ALWAYS has an associated quantity (just not necessarily an associated weight) you should use the first query instead, an inner join for the quantity, and an outer join for the weight. (it all depends on your situation)

multiple mysql tables with multiple joins

I have 4 mysql tables as follows:
products:
----------------------------------------------------
product_id product_name price discount
----------------------------------------------------
1 product 1 10.00 1.00
2 product 2 20.00 2.00
3 product 3 25.00 1.00
----------------------------------------------------
subcategory
----------------------------------------------------
cb_category_id subcategory_name status
----------------------------------------------------
1 subcat 1 Enabled
2 subcat 2 Disabled
3 subcat 3 Enabled
------------------------------------------------------
temp_products
------------------------------------------------------
id productid catid
------------------------------------------------------
1 1 1
2 1 2
3 2 1
------------------------------------------------------
product_images
------------------------------------------------------
product_id images
------------------------------------------------------
1 image1.jpg
1 image2.jpg
2 image2-1.jpg
--------------------------------------------------------
temp_products.catid and subcategory.cb_category_id
and
temp_products.productid and products.product_id
and
products.product_id and product_images.product_id
are related..A product can have multiple images.
I wish to have a subcategory selected with all products with first image for a product from product_images and WHERE subcategory.status is "Enabled"...?? Need to limit output to only 1 cb_category_id with multiple product_id under it, like as follows:
----------------------------------------------------------------------------------
cb_category_id subcategory_name product_id product_name, price, discount, images
-----------------------------------------------------------------------------------
1 subcat 1 1 product 1 10.00 1.00 image1.jpg
1 subcat 1 2 product 2 20.00 2.00 image2- 1.jpg
My query is as follows:
SELECT p.product_id,p.product_name,p.price,p.discount,s.cb_category_id,s.subcategory_name
FROM products p,subcategory s
INNER JOIN temp_products ON p.product_id = temp_products.productid
INNER JOIN temp_products tp ON tp.catid = s.cb_category_id
WHERE tp.catid = s.cb_category_id
I am getting unknown column p.product_id in on clause ....Regarding including images i am at a dead end.
Help requested...I am unable to comprehend the joins required for the same...
you have a typo . you dont have products_id in your products table.
change this
p.products_id
to
p.product_id
EDIT: rewrite your query like that :
SELECT p.product_id,p.product_name,p.price,p.discount,s.cb_category_id,s.subcategory_name ,pi.images
FROM products p
INNER JOIN temp_products tp ON p.product_id = tp.productid
INNER JOIN product_images pi ON p.product_id = pi.product_id
INNER JOIN subcategory s ON tp.catid = s.cb_category_id
GROUP BY p.product_id
Your 'temp_products' table is basically a mapping table for 'products' and 'subcategory'. I have not tested the following code but you'll get the idea from comments(text after # character). this should solve your problem:
SELECT p.product_id,p.product_name,p.price,p.discount,s.cb_category_id,s.subcategory_name,pim.images
FROM products p
INNER JOIN temp_products tp ON p.product_id = tp.productid #remove duplicate join
INNER JOIN subcategory s ON tp.catid = s.cb_category_id
LEFT JOIN product_images pim ON pim.product_id =p.product_id #join for images
WHERE s.status = 'Enabled' AND #subcategory is enabled
s.cb_category_id = '1' #subcategory you want to be selected
GROUP BY p.product_id ORDER BY p.product_id, pim.images;

Select sold and unsold products

I'm trying to select all products names and how many has been sold in current date, but I'm having a problem since not all products are sold everyday. (When the product has not been sold it must return 0)
TABLE PRODUCTS
ID NAME
1 APPLE
2 PINEAPPLE
3 COFFE
TABLE SALES
ID DATE
1 2014-01-13
2 2014-01-13
TABLE PRODUCTS_AND_SALES
SALE_ID PRODUCT_ID AMOUNT
1 3 2
1 1 1
2 3 1
What I expect to receive:
PRODUCT AMOUNT
APPLE 1
PINEAPPLE 0
COFFE 3
What I receive:
PRODUCT AMOUNT
APPLE 1
COFFE 3
My query:
select product, sum(amount) from products
join products_and_sales using (product_id)
join sales using (sale_id)
where date(dt_sale) = curdate()
group by product_id;
try this
select name as PRODUCT , ifnull(sum(AMOUNT),0) amount from products p
left join PRODUCTS_AND_SALES ps
on p.id = ps.PRODUCT_ID
group by product
DEMO HERE
EDIT:
if you wanna use specefic date then use this
select name as PRODUCT ,if(date = '2014-01-13' ,ifnull(sum(AMOUNT),0), 0 ) amount
from products p
left join PRODUCTS_AND_SALES ps on p.id = ps.PRODUCT_ID
left join SALES s on s.id = ps.SALE_ID
group by product
DEMO HERE
just replace this date 2014-01-13 by the date you want. (curdate()) or what ever
Use OUTER JOIN instead of INNER JOIN
if you show the query you use we will correct it for you.
Try this...
SELECT pr.NAME, IFNULL(SUM(prs.AMOUNT), 0)
FROM PRODUCTS pr
LEFT OUTER JOIN PRODUCTS_AND_SALES prs ON pr.id = prs.product_id
LEFT OUTER JOIN SALES sl ON sl.id = prs.sales_id AND date(s1.dt_sale) = curdate()
GROUP BY pr.NAME

MYSQL count related rows in 2 tables

I have 3 tables
products table
productid productname
--------- -----------
1 product 1
2 product 2
5 product 3
10 product 4
11 product 5
12 product 6
accounts_products table
id productid accountid
-- --------- ---------
1 1 accountid 1
2 10 accountid 2
3 2 accountid3
leads_products table
id productid leadid
-- --------- ---------
1 1 leadid 1
2 5 leadid 2
3 2 leadid 3
I am trying to count how many total products are in leads_products and accounts_products tables based on the same productid's.
Expected result
Product ID Product Name Total
----------- ------------ --------
1 product 1 2
2 product 2 2
5 product 3 1
10 product 4 1
I tried so far
SELECT p.productid as 'Product ID',
p.productname as 'Product Name',
COUNT(*) as 'Total' FROM products p
INNER JOIN leads_products l ON (l.productid=p.productid)
INNER JOIN accounts_products a ON (a.productid=p.productid)
GROUP BY p.productname,p.productid
Above query counts and display higher number than expected.
I hope it makes sense.
Try this:
SELECT p.productid as 'Product ID',
p.productname as 'Product Name',
(SELECT COUNT(*)
FROM leads_products AS l
WHERE l.productid = p.productid) +
(SELECT COUNT(*)
FROM accounts_products AS a
WHERE a.productid=p.productid) AS 'Total'
FROM products AS p
http://www.sqlfiddle.com/#!2/f8472/5
Alternative approach using JOIN (better performance):
SELECT p.productid as 'Product ID',
p.productname as 'Product Name',
IFNULL(l.count, 0) + IFNULL(a.count, 0) as 'Total'
FROM products AS p
LEFT JOIN (
SELECT productid, COUNT(*) AS count
FROM leads_products
GROUP BY productid
) AS l
ON l.productid = p.productid
LEFT JOIN (
SELECT productid, COUNT(*) AS count
FROM accounts_products
GROUP BY productid
) AS a
ON a.productid = p.productid
http://www.sqlfiddle.com/#!2/f8472/33
I did it with a left join of p.products to preserve all the products then counted the products in each list and added them together. Worked first try. There's probably a billion ways to do this.
SELECT p.productid 'Product ID',
p.productname 'Product Name',
COUNT(a.accountid) + COUNT(l.leadid) total
FROM products p
LEFT JOIN accounts_products a ON p.productid = a.productid
LEFT JOIN leads_products l ON p.productid = l.productid
GROUP BY p.productid
http://www.sqlfiddle.com/#!2/f8472/37

mysql count, distinct, join? Confusion

i have 2 tables:
tblItems
ID | orderID | productID
1 1 2
2 1 2
3 2 1
4 3 2
tblProducts
productID | productName
1 ABC
2 DEF
im attempting to find the most popular Product based on whats in "tblItems", and display the product Name and the number of times it appears in the tblItems table.
i can get mysql to count up the total like:
$sql="SELECT COUNT(productID) AS CountProductID FROM tblItems";
but i can't figure out how to join the products table on..if i try LEFT JOIN the query goes horribly wrong
hopefully thats not too confusing..thankss
Are you simply trying to find the count of orders by product like so:
Select P.ProductName, Count(*)
From tblItems As I
Join tblProducts As P
On P.ProductId = I.ProductId
Group By P.ProductName
I think you may be looking for something like:
SELECT tblProducts.productName, COUNT(tblItems.ID)
FROM tblProducts
LEFT JOIN tblItems USING(productID)
GROUP BY tblProducts.productID
SELECT count(i.productID) AS cnt, p.productName FROM tblItems AS i
LEFT JOIN tblProducts AS p ON p.productID = i.productID
GROUP BY i.productID
ORDER BY cnt desc