2011-01-05 3 views
2

I가 다음과 같은 테이블 :SQL에 합류 데이터

항목 (표) 없는 이름 가격 설명

항목 - 사용자 정의 (표) 설명 1 내용 입력란을 항목 ID 사용자 ID

항목이 항목 - 사용자 정의 표에 설명이있는 경우 그 설명을 표시하고 싶습니다. 항목 t의 설명을 표시하고 있습니다. 할 수 있는.

나는 내부에서 item.no = item-custom.itemid에 대한 항목 - 사용자 정의 테이블을 조인하는 쿼리를 만들었습니다. 항목에 항목 - 사용자 정의 테이블에 설명이있는 경우이 작업은 올바르게 수행됩니다. 그러나 설명이 없으면 쿼리가 데이터를 반환하지 않습니다.

이 쿼리는 어떻게 작성해야 항목 작성 테이블에 설명이 있는지에 상관없이 항상 항목 레코드를 얻을 수 있습니다. 여기

내가 무엇을 가지고 :

SELECT item.description, item-custom.description1, item-custom.description 
FROM item 
INNER JOIN item-custom ON item.no = item-custom.itemid 

답변

3

당신은 왼쪽을 사용하여이 작업을 수행 할 수있는 곳으로 조인 시도 가입 할 수 있습니다. 왼쪽 조인에 대해 자세히 읽을 수 있습니다. here 내부 조인은 null이 허용되지 않는 두 테이블의 레코드 만 가져옵니다. 따라서 description이 비어 있으면 (NULL) 레코드가 표시되지 않습니다. 왼쪽 결합을 사용하면됩니다.

SELECT item.description, item-custom.description1, item-custom.description 
FROM item 
LEFT JOIN item-custom ON item.no = item-custom.itemid 
2
SELECT item.description, item-custom.description1, item-custom.description 
FROM item 
LEFT OUTER JOIN item-custom ON item.no = item-custom.itemid 
0

대신 내부의 절 대신

SELECT item.description, item-custom.description1, item-custom.description 
FROM item as item , item-custom as item-custom 
WHERE item.no = item-custom.itemid 
0
SELECT CASE WHEN item-custom.description2 IS NOT NULL 
THEN item-custom.description2 
ELSE item.description END, ... 
1

나는 그것이 당신의 조건에 더 맞는 생각

는 SQL 서버 :

SELECT ISNULL(item.description, item-custom.description) as descriptin 
FROM item 
LEFT OUTER JOIN item-custom ON item.no = item-custom.itemid 

MySQL의

SELECT COALESCE(item.description, item-custom.description) as descriptin 
FROM item 
LEFT OUTER JOIN item-custom ON item.no = item-custom.itemid 

ISNULL 또는 최초의 비 NULL 인수를 돌려줍니다 COALESCE