2012-07-30 3 views
1

SQL에서 좋지 않으므로 이러한 기본 질문에 대해 저를 꾸짖지 마십시오. 현재 다른 기술에 대한 많은 숙제를하고 있으므로 SQL을 향상시킬 것이며 시간을 할애 할 것입니다. 어쨌든 요점을 알려주십시오.conditon이 만족스럽지 않더라도 데이터를 가져 오는 sql 쿼리

내 질문에 대한 질문입니다. 나는 세 테이블에서 데이터를 가져 오려고 노력하고 그것은 모든 조건이 만족하지만 문제가 게시에 대한 그것은 이미지가 존재하지 않는 즉, 이미지 테이블에 게시에 대한 기록되지 않을 수 있습니다 그때 post.post_id = img.post_id and img.sequence='1' 조건 실패 및 행은 반환되지 않지만 게시 할 이미지가없는 경우에도 행을 반환해야합니다. 이 경우 이미지 경로 열에 대해 null을 반환합니다.

SELECT 
    img.path as path, pav.value_text as beds, post.post_id as postID 
FROM 
    postings post, post_attributes_values pav, images img 
where 
    post.post_id = pav.post_id and post.status !='expired' and pav.attr_id='33' and post.post_id = img.post_id and img.sequence='1' and post.post_id=49 

처럼 다음 O/P이어야 post.post_id = img.post_id and img.sequence='1' 조건을 만족하지 않을 경우 모든 조건을 만족 한 후 O/P는

path    beds   postId 
----------------------------------------------- 
    saome path value  2    49 

처럼되어 있지만, 경우

path  beds   postId 
---------------------------------------- 
    null   2    49 

답변

1

사용이 왼쪽 외부

가입
SELECT 
    img.path as path, pav.value_text as beds, post.post_id as postID 
FROM 
    postings post 
    inner join post_attributes_values pav on post.post_id = pav.post_id and pav.attr_id='33' 
    left outer join images img on post.post_id = img.post_id and img.sequence='1' 
where post.status !='expired' and post.post_id=49 
+0

S '', 침대와 같은 경로 pav.value_text 같은 img.path를 선택 전기 이너 가 post_attributes_values가 post.post_id = pav.post_id 외측에 가입 post.post_id 이미지 IMG에 남아 PAV 가입 골대 postID 로 post.post_id = img.post_id 및 img.sequence = '1' 여기서 post.status! = '만료되었습니다'및 pav.attr_id = '33 '및 post.post_id = 50''이 나와 나를 위해 일했습니다. ** 고맙습니다 **. –

관련 문제