테스트 사이트 - 개발 중인 베타 버전입니다

sql 출력 관련 재질문 채택완료

웹프리죤 5년 전 조회 1,858

my_information 테이블에 type1 ~ type13 까지 모든 컬럼에 값이 하나도 없을 때

아래처럼 어거지로....처리를 해서 원하는데로 되긴 했는데

 

더 간결한 정상적인.. sql 조건문을 적용시킬 방법 도움을 받고자 합니다.

 

$sql_common = " from g5_write_recruits ";

 

/* 맞춤정보 조건문 */ $sql = " select * from my_information where mb_id = '{$member['mb_id']}' "; $sch = sql_fetch($sql);

if($sch['type1'] == '' && $sch['type2'] == '' && $sch['type3'] == '' && $sch['type4'] == '' && $sch['type5'] == '' && $sch['type6'] == '' && $sch['type7'] == '' && $sch['type8'] == '' && $sch['type9'] == '' && $sch['type10'] == '' && $sch['type11'] == '' && $sch['type12'] == '' && $sch['type13'] == '') {

    $sql_search = " where none = 'none' "; // 어거지로 .. 없는 컬럼으로 출력이 안되게 해놨음 ..

} else {

$sql_search = "   where type1 like '%{$sch['type1']}%'     and type2 like '%{$sch['type2']}%'     and type3 like '%{$sch['type3']}%'     and type4 like '%{$sch['type4']}%'     and type5 like '%{$sch['type5']}%'     and type6 like '%{$sch['type6']}%'     and type7 like '%{$sch['type7']}%'     and type8 like '%{$sch['type8']}%'     and type9 like '%{$sch['type9']}%'     and type10 like '%{$sch['type10']}%'     and type11 like '%{$sch['type11']}%'     and type12 like '%{$sch['type12']}%'     and type13 like '%{$sch['type13']}%'     ";

}

 

$sql = " select * {$sql_common} {$sql_search} {$sql_order} limit {$from_record}, {$rows} "; $result = sql_query($sql);

댓글을 작성하려면 로그인이 필요합니다.

답변 2개

채택된 답변
+20 포인트
e
5년 전

$sql_search = " where 0 ";

또는

$sql_search = " where 1=0 ";

통상 이렇게 사용합니다.

로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

5년 전

하시고자 하는게 아래와 같나요?

1. my_information 의 각 필드에 해당하는 값으로 g5_write_recruits 테이블을 해당 필드 조회 조건을 넣어 조회

2. my_information 테이블의 필드 조건이 모두 없으면  g5_write_recruits 테이블에도 조회 조건이 안되어 결과가 안 나오도록 한다.

 

참고로 where 절의 조건은 true/false로 해석하여 처리됩니다.

만일 조회조건 키워드가 비어 있어서  type1 like '%%'  처럼 주어지면 모든 레코드가 해당되겠죠(like 문은 각 레코드에서 조건이 맞으면 true 로 해석됩니다.) 

그래서 테이블의 모든 레코드가 조회 되는 것이구요. my_information 테이블의 조회 조건 컬럼들이 모두 비어있다면 and type2 like '%{$sch['type2']}%'  와 같이 작성한 부부은 모두  like '%%' 이니 모든 레코드가 true로 해석 되어 g5_write_recruits 의 모든 레코드가 조회 됩니다.

조회조건이 모두 비어 있을때 아무 것도 안나오게 하려면 조건이 모두 비어있을 때  false 조건이 추가되게 해주면 되겠죠.( and false)

my_information 테이블의 조회 조건이 모두 문자형인 듯하니

$sql = " select *,(case when (type1+type2+......+type13) = '' then 0 else 1 end) as blank_condition

           from my_information

           where mb_id = '{$member['mb_id']}' ";

 

이렇게 모든 조건이 비었을 경우 0 으로 아니면 1 로 세팅되는 필드 추가하시고 

어거지로 출력하시는 if 문 삭제하시고 아래 처럼 조회 조건 마지막에 blank_condition 을 추가하시면 될 것 같네요. ( (type1+type2+......+type13) 이 부분은 type1~type13까지 더한다는 뜻입니다.)

$sql_search = "   where type1 like '%{$sch['type1']}%'     and type2 like '%{$sch['type2']}%'     and type3 like '%{$sch['type3']}%'     and type4 like '%{$sch['type4']}%'     and type5 like '%{$sch['type5']}%'     and type6 like '%{$sch['type6']}%'     and type7 like '%{$sch['type7']}%'     and type8 like '%{$sch['type8']}%'     and type9 like '%{$sch['type9']}%'     and type10 like '%{$sch['type10']}%'     and type11 like '%{$sch['type11']}%'     and type12 like '%{$sch['type12']}%'     and type13 like '%{$sch['type13']}%'

    and 1 = {$sch['blank_condition']}       <==== 추가     ";

 

 

로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인