Answer the question
In order to leave comments, you need to log in
Multiple joins with condition?
SELECT
t1.id,
t1.name AS product,
t3.name AS color,
t5.name AS size
FROM
`product` AS t1
JOIN
`product_color` AS t2 ON t1.id = t2.product_id
JOIN
`color` AS t3 ON t2.color_id = t3.id
JOIN
`product_size` AS t4 ON t1.id = t4.product_id
JOIN
`size` AS t5 ON t4.size_id = t5.id
WHERE
(color = "black" and size = "S")
and
(color = "black" and size = "M")
and
(size = "L")
Answer the question
In order to leave comments, you need to log in
In my opinion, everything is possible :-) because
I would do it through subquery (convenient for sampling according to different conditions from a compiled "set" of intersections)
select BSproduct from
( -- Список продуктов имеющих черный цвет и малый размер
select product as BSProduct from
(
SELECT t1.id, t1.name AS product, t3.name AS color, t5.name AS size
FROM `product` AS t1
JOIN `product_color` AS t2 ON t1.id = t2.product_id
JOIN `color` AS t3 ON t2.color_id = t3.id
JOIN `product_size` AS t4 ON t1.id = t4.product_id
JOIN `size` AS t5 ON t4.size_id = t5.id
)
where color = "black" and size = "S"
) as BlackAndSmallSize,
( -- Список продуктов имеющих черный цвет и средний размер
select product as BMProduct from
(
SELECT t1.id, t1.name AS product, t3.name AS color, t5.name AS size
FROM `product` AS t1
JOIN `product_color` AS t2 ON t1.id = t2.product_id
JOIN `color` AS t3 ON t2.color_id = t3.id
JOIN `product_size` AS t4 ON t1.id = t4.product_id
JOIN `size` AS t5 ON t4.size_id = t5.id
)
where color = "black" and size = "M"
) as BlackAndMidSize,
( -- Список продуктов имеющих большой размер
select product as LProduct from
(
SELECT t1.id, t1.name AS product, t3.name AS color, t5.name AS size
FROM `product` AS t1
JOIN `product_color` AS t2 ON t1.id = t2.product_id
JOIN `color` AS t3 ON t2.color_id = t3.id
JOIN `product_size` AS t4 ON t1.id = t4.product_id
JOIN `size` AS t5 ON t4.size_id = t5.id
)
where size = "L"
) as LargeSize
where BSProduct=BMProduct and BMProduct=LProduct
(
SELECT t1.id, t1.name AS product, t3.name AS color, t5.name AS size
FROM `product` AS t1
JOIN `product_color` AS t2 ON t1.id = t2.product_id
JOIN `color` AS t3 ON t2.color_id = t3.id
JOIN `product_size` AS t4 ON t1.id = t4.product_id
JOIN `size` AS t5 ON t4.size_id = t5.id
)
so write as you said:
WHERE
(t3.name = 'black' and t5.name = 'S')
and
(t3.name = 'black' and t5.name = 'M')
and
(t5.name = 'L')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question