2013-05-28 2 views
0

나는 내 PHP 스크립트에 대한 쿼리를 작성해 왔으며 원하는 작업을 수행 할 방법이 확실하지 않습니다. 조인이 너무 끔찍해서 어떻게해야하는지 잘 모르겠습니다.MySQL 쿼리 관련 문제

두 개의 테이블, 항목 및 범주가 있습니다.

품목에는 recno, sku, 묘사, 가격, 상표, 종류, 세부 사항, 성, 크기, 색깔, dateadded가있다. 카테고리에는 recno, category, parent가 있습니다.

내가 필요로하는 쿼리, 그 범주 X이며, 그 카테고리의 상위 내가

SELECT DISTINCT items.recno, 
       items.sku, 
       items.description, 
       items.price, 
       items.brand, 
       items.category, 
       items.details, 
       items.gender, 
       items.size, 
       items.color, 
       items.dateadded 
FROM `items` 
     INNER JOIN `categories` 
       ON items.category = categories.parent 
ORDER BY `description` 

을 시도했습니다 X.

하지만 그건 그냥 모든 선택 항목을 선택해야합니다. 조인을 사용하려고했지만 아이 카테고리에서 항목을 가져올 수 없었습니다.

이 문제에 대한 도움을 주시면 대단히 감사하겠습니다.

답변

2

이 시도하십시오

SELECT DISTINCT 
    items.recno, 
    items.sku, 
    items.description, 
    items.price, 
    items.brand, 
    items.category, 
    items.details, 
    items.gender, 
    items.size, 
    items.color, 
    items.dateadded 
FROM `items` 
JOIN `categories` ON items.category = categories.parent 
WHERE categories.category='x' AND categories.parent='X' 

쿼리에 WHERE 조건을 추가하지 않은 결과가 모든 행

빈 결과를 반환
+0

을 보여 그 이유는. – sharf

+0

where what categories.parent = 'X'? – Nisam

+0

그런 종류의 작품이지만 하위 카테고리가 아닌 해당 카테고리의 항목 만 선택합니다. – sharf