2013-08-29 2 views
0

아래 필드가있는 테이블이 있습니다.사례 명세서에 여러 조건이 있습니다.

id, user_id, shipping_type, inv_type, invoice_no.

inv_type은 ENUM (지역/국제)입니다.

나는 각 행의 송장을 받고 싶습니다. inv_type = 'local'이라면 invoice_no에 alias를 'local_inv_no'로 지정하고, international이면 'int_inv_no'를 별칭으로 사용합니다. 둘 중 하나가 null이면 null로 'local_inv_no'또는 결과로 null로 int_inv_no가 표시됩니다.

다음은 최근 시도입니다.

$sql = "select case when b.inv_type='local' 
     then (case when b.invoice_no is NULL then 'n' else b.invoice_no end 'local_inv') 
     else 
     when n.inv_type='int' then (case when b.invoice_no is NULL then 'n' else b.invoice_no end 'int_inv') 
     end 'inv_status' 
     from user_invoice b 
     where b.user_id = '$active_id' 
     "; 

그러나 결과는 나에게 줄 것 같습니다.

+0

원하는 결과와 함께 DDL 및/또는 sqlfiddle을 제공하는 것이 좋습니다. 그리고 "결과를주지 않는다"? 이 쿼리는 오류 메시지를 생성합니다! – Strawberry

답변

3

2 개의 열을 갖고 싶은 것처럼 이해하셨습니까? 그런 다음 하나의 case when에 넣지 마십시오. 결과가 하나의 열에 표시됩니다.

select 
if(b.inv_type='local', coalesce(b.invoice_no, 'n'), NULL) as local_inv_no, 
if(b.inv_type='international', coalesce(b.invoice_no, 'n'), NULL) as int_inv_no 
from user_invoice b 
where b.user_id = '$active_id' 

coalesce() 기능은 case when 구조의 짧은 형태이다. not null 인 매개 변수 중 첫 번째를 반환합니다.

if()if(<what_to_check>, <then>, <else>)이므로 case when보다 약간 작습니다.

0

많이 닮은 문장이 닮았습니다.

난 당신이 acommplisch하려고 알고 downt 때문에, 나는 내가 u는 당신의 문에 'inv_status'와 'int_inv을'왜 파악하지 못할 작업 문을

select 

case when b.inv_type='local' 
     then (
       case when b.invoice_no is NULL 
       then 
        'n' 
       else 
        b.invoice_no 
       end) 
     else 
     (
     case when n.inv_type='int' 
      then 
      (
      case when b.invoice_no is NULL 
       then 
        'n' 
       else 
        b.invoice_no 
       end 
       ) 

     end 
     ) 
end 
from user_invoice b 
where b.user_id = '$active_id' 

를 재구성하기 위해 최선을 시도해보십시오. 하지만 Mimer SQL로 확인한 바에 따르면 완전히 작동하는 SQL 문

관련 문제