programing

SQL many to many select

muds 2023. 9. 13. 23:58
반응형

SQL many to many select

category_product
---------------
id_category
id_product

product
---------------
id_product
id_manufacturer

manufacturer
---------------
id_manufacturer
name

id_category가 어떤 것과 같을 때 제조업체의 모든 이름을 선택하도록 SQL 쿼리를 만드는 방법은 무엇입니까?

테이블 내부의 간단한 결합입니다.

SELECT m.name, cp.id_category
FROM manufacturer as m
INNER JOIN product as p
    ON m.id_manufacturer = p.id_manufacturer
INNER JOIN category_product as cp
    ON p.id_product = cp.id_product
WHERE cp.id_category = 'some value'

조인이 없는 쿼리는 다음과 같습니다.

SELECT m.name 
FROM manufacturer as m, product as p, category_product as cp 
WHERE cp.id_category = <your value>
      AND cp.id_product = p.id_product 
      AND p.id_manufacturer = m.id_manufacturer 
Select M.name
From   manufacturer M
Where  M.id_manufacturer in ( Select P.id_manufacturer
                              From   product P
                              Where  P.id_product in ( Select C.id_product
                                                       From   category_product C
                                                       Where  C.id_category = ?))

다음과 같은 것을 시도해보세요.

SELECT  m.*
FROM      category_product cp INNER JOIN
           product p ON cp.id_product = p.id_product INNER JOIN
           manufacturer m ON p.id_manufacturer = m.id_manufacturer
WHERE      cp.id_category = <your_value>
SELECT m.name, cp.id_category
FROM manufacturer AS M INNER JOIN product AS P
    ON M.id_manufacturer = M.id_manufacturer
 INNER JOIN category_product AS CP
    ON P.id_product = CP.id_product 
WHERE cp.id_category = 'add value'

언급URL : https://stackoverflow.com/questions/3486644/sql-many-to-many-select

반응형