Задать вопрос
  • Множественные объединения c условием?

    @nozzy
    Symfony, Laravel, SQL, Python, Telegram
    select 
    t1.*,
    t3.name 
    from 
    product t1
    inner join (
    	select 
    	product_id
    	from product_size
    	inner join size on id = size_id
    	where name in ('S', 'M', 'L')
    	group by product_id
    	having count(product_id) = 3
    ) t2 on t2.product_id = t1.id
    inner join (
    	select 
    	product_id 
    	,name
    	from product_color
    	inner join color on id = color_id
    	where name = 'black'
    ) t3 on t3.product_id = t2.product_id
    Ответ написан
    3 комментария
  • Множественные объединения c условием?

    @Joysi75
    По моему все можно :-) ибо
    Я бы делал через subquery (удобно для выборок по разным условиям из составленного "набора" пересечений)
    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


    Не знаю как насчет MySQL и его форков, в некоторых SQL серверах можно заранее задавать подзапросы типа
    (
            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
          )

    с помощью команды WITH - тогда объем текста выборки сократится в разы.
    Ответ написан
    2 комментария