본문 바로가기

Programming/sqlite

sqlite 에서의 if문.


특별히, if문은 존재하지 않는듯.

공식 홈페이지 가면, if문 대신 case문을 쓰라고 한다. 상세 설명은 다음과 같음...
읽어볼 사람은 읽어보시고, 

The CASE expression

A CASE expression serves a role similar to IF-THEN-ELSE in other programming languages.

The optional expression that occurs in between the CASE keyword and the first WHEN keyword is called the "base" expression. There are two basic forms of the CASE expression: those with a base expression and those without.

In a CASE without a base expression, each WHEN expression is evaluated and the result treated as a boolean, starting with the leftmost and continuing to the right. The result of the CASE expression is the evaluation of the THEN expression that corresponds to the first WHEN expression that evaluates to true. Or, if none of the WHEN expressions evaluate to true, the result of evaluating the ELSE expression, if any. If there is no ELSE expression and none of the WHEN expressions are true, then the overall result is NULL.

A NULL result is considered untrue when evaluating WHEN terms.

In a CASE with a base expression, the base expression is evaluated just once and the result is compared against the evaluation of each WHEN expression from left to right. The result of the CASE expression is the evaluation of the THEN expression that corresponds to the first WHEN expression for which the comparison is true. Or, if none of the WHEN expressions evaluate to a value equal to the base expression, the result of evaluating the ELSE expression, if any. If there is no ELSE expression and none of the WHEN expressions produce a result equal to the base expression, the overall result is NULL.

When comparing a base expression against a WHEN expression, the same collating sequence, affinity, and NULL-handling rules apply as if the base expression and WHEN expression are respectively the left- and right-hand operands of an = operator.

If the base expression is NULL then the result of the CASE is always the result of evaluating the ELSE expression if it exists, or NULL if it does not.

Both forms of the CASE expression use lazy, or short-circuit, evaluation.

The only difference between the following two CASE expressions is that the x expression is evaluated exactly once in the first example but might be evaluated multiple times in the second:

  • CASE x WHEN w1 THEN r1 WHEN w2 THEN r2 ELSE r3 END
  • CASE WHEN x=w1 THEN r1 WHEN x=w2 THEN r2 ELSE r3 END


출처 >  http://www.sqlite.org/lang_expr.html#case 

주저리주저리, 나와있기는 한데.. 그닥 어려운 영어와 내용은 아니다. 아래 사용예 보고 응용하여 사용하면 될듯...

사용예 > 

SELECT case when(rowid==1) then "1이다" else "1이 아니다" end FROM main




 

'Programming > sqlite' 카테고리의 다른 글

Sqlite 문자 합치기.  (0) 2012.01.24
Sqlite에서의 Replace문.  (2) 2012.01.24
SQLite 에서의 NULL 체크  (0) 2012.01.24