multiple mysql tables with multiple joins - mysql

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;

Related

Combine results from different queries

I have a simplified table setup as following:
Table 1 (products)
product_id product_parent_id
646 45
Table 2 (category)
product_id category_name
45 category_1
Table 3 (product_names)
product_id slug
45 product45-details
646 product646-details
For product_id 646 I would like to get the category of product_id 45 and the slug of product 646 and combine them into one output.
So basically the result should be:
id link
646 category_1/product646-details-detail
So far I have the following:
select product_id, CONCAT('I.category_name,'/',E.slug,'-detail') as link from products C left join product_names E on C.product_parent_id=E.product_id left join category H on E.product_id=H.product_id where C.product_id > 1 group by C.product_id
What would be the approach to get the result for E.slug based on the product_id and not the parent_parent_id
You can join:
select p.product_id, c.category_name, pn.slug
from products p
inner join category c on c.product_id = p.product_parent_id
inner join product_names pn on pn.product_id = p.product_id
where p.product_id = 646

php - Check same column twice in mysqli query

I have 3 tables in my php based system.
Those tables are product, category, product_categories.
Product
pid | product_name | price
1 | Nike T-Shirt | 23
Category
cid | category_name
1 | Men
2 | Women
Product_categories
pcid | cid | pid
1 | 1 | 1
2 | 2 | 1
That means, 1 product may be in both multiple categories.
Now I am developing the product search section with filter.
If a user select both categories, all the products in selected categories should display.
Example : If a user select both Men & Women, Nike T-Shirt should be displayed.
The query I used:
select p.*
from products p
left join product_categories pc on pc.pid=p.pid
WHERE pc.cid ='1' AND pc.cid = '2'
But it not returning correct products.
Where is the error?
You want conditions that span over multiple rows, which suggests aggregation. You can join the tables, group by product, and use the having clause for filtering:
select p.id, p.product_name, p.price
from product p
inner join product_category pc on pc.pid = p.pid
inner join category c on c.cid = pc.cid
where c.category_name in ('Men', 'Women')
group by p.pid, p.product_name, p.price
having max(c.category_name = 'Men') = 1 and max(c.category_name = 'Women') = 1
If you can filter by category id rather than by category name, then you need one less join:
select p.id, p.product_name, p.price
from product p
inner join product_category pc on pc.pid = p.pid
where pc.cid in (1, 2)
group by p.pid, p.product_name, p.price
having max(pc.cid= 1) = 1 and max(pc.cid = 2) = 1
Here are few alternatives:
-- All products that are at lest in one of the desired categories
select * from product where product.pid = any (select product_category.pid from product_category where cid = 1 or cid = 2);
-- Products that are in both categories
select * from product where product.pid = any (
select product_category.pid from product_category where cid = 1 or cid = 2 group by pid having count(cid) = 2
);
-- Products with additional information "in how many desired categories they are"
-- You can order by it and/or filter on it
with product_category_matches as (
select pid, count(*) as category_count from product_category where cid in (1, 2) group by pid
)
select *, product_category_matches.category_count from
product inner join product_category_matches on product.pid = product_category_matches.pid
where product_category_matches.category_count > 0 -- Product must be at least in one desired category
order by product_category_matches.category_count desc

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)

Join two table. One column using SUM

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

How to get all the data from two tables?

I have two tables
1.Products
prod_id prod_name
1 honda
2 hero
3 marcedes
4 audi
2.Product to category
cat_id prod_id
1 1
1 2
2 3
2 4
Now i want result like below
prod_id prod_name cat_id
1 honda 1
2 hero 1
3 marcedes 2
4 audi 2
I cannot seem to figure this out. Any help greatly appreciated!
select products.*, category.cat_id
from products, category
where products.prod_id = category.prod_id
select products.prod_id ,products.prod_name, category.cat_id
from products, category
where category.prod_id = products.prod_id
select p.prod_id,p.prod_name,c.cat_id
from Products p inner join category c
on p.prod_id = c.prod_id
Simply use a join, if you only want products has a cat_id, then change LEFT JOIN to INNER JOIN.
SELECT t1.prod_id, t1.prod_name, t2.cat_id
FORM Products t1
LEFT JOIN ProductToCategory t2 ON t1.prod_id = t2.prod_id
select p.prod_id, p.prod_name,c.catid
from products p
inner join prodtocat pc on p.prod_id = pc.prod_id