• Aucun résultat trouvé

T HE C ASE E XPRESSION

Dans le document SQL for MySQL Developers (Page 128-133)

Common Elements

Exercise 5.12: Find the numbers of the players who became committee members today

5.8 T HE C ASE E XPRESSION

---BOOKSQL@localhost

Example 5.19:Show the penalties that were paid today.

SELECT *

FROM PENALTIES

WHERE PAYMENT_DATE = CURRENT_DATE

Obviously, this statement has an empty result because your computer clock will undoubtedly show the present date and time, whereas most penalties were incurred before the year 2000.

Exercise 5.11: What are the differences between user and system variables?

Exercise 5.12: Find the numbers of the players who became committee members today.

5.8 T

HE

C

ASE

E

XPRESSION

A special scalar expression is the case expression.This expression serves as a kind ofIF-THEN-ELSE statement. It can be compared with the SWITCHstatement in Java and the CASEstatement in Pascal.

DEFINITION

<case expression> ::=

CASE <when definition> [ ELSE <scalar expression> ] END

<when definition> ::= <when definition-1> | <when definition-2>

<when definition-1> ::=

<scalar expression>

WHEN <scalar expression> THEN <scalar expression>

[ WHEN <scalar expression> THEN <scalar expression> ]...

<when definition-2> ::=

WHEN <condition> THEN <scalar expression>

[ WHEN <condition> THEN <scalar expression> ]...

Each case expression starts with a when definition.Two forms of when defini-tions exist. The easiest way to explain the possibilities of the first is through a few examples.

Example 5.20: Get the player number, the sex, and the name of each player who joined the club after 1980. The sex must be printed as 'Female'or'Male'.

SELECT PLAYERNO,

Explanation: This construct is equal to the following IF-THEN-ELSEconstruct:

IF SEX = 'F' THEN RETURN 'Female' ELSE

RETURN 'Male' ENDIF

The data type of the case expression depends on the data types of the expres-sions that follow the words THENandELSE. The data types of these expressions must all be the same, or an implicit cast is performed (see Section 5.11 for an explanation of casting). An error message is returned if the data types of the values do not match.

As the definition shows, ELSE is not required. The previous case expression could also have been formulated as follows:

CASE SEX

WHEN 'F' THEN 'Female' WHEN 'M' THEN 'Male' END

In this case, if ELSEis omitted and the value of the SEXcolumn is not equal to one of the scalar expressions in a when definition (which is not possible), the null value is returned.

SELECT PLAYERNO, CASE SEX

WHEN 'F' THEN 'Female' END AS FEMALES, NAME

Explanation: A column name is specified to give the second result column a meaningful name.

Many when conditions can be included in a case expression.

CASE TOWN

With the case expression, we can create very powerful SELECT clauses, espe-cially, if we start to nest case expressions:

CASE TOWN

Example 5.21: Use both case expressions shown previously in a SELECT statement.

SELECT PLAYERNO, TOWN, BIRTH_DATE, CASE TOWN ---2 Stratford 1948-09-01 0 Old Stratforder 6 Stratford 1964-06-25 0 Young Stratforder 7 Stratford 1963-05-11 0 Young Stratforder 8 Inglewood 1962-07-08 2 Old Inglewooder 27 Eltham 1964-12-28 3 Rest

28 Midhurst 1963-06-22 3 Rest

39 Stratford 1956-10-29 0 Young Stratforder 44 Inglewood 1963-01-09 2 Young Inglewooder 57 Stratford 1971-08-17 0 Young Stratforder 83 Stratford 1956-11-11 0 Young Stratforder 95 Douglas 1963-05-14 3 Rest

100 Stratford 1963-02-28 0 Young Stratforder 104 Eltham 1970-05-10 3 Rest

112 Plymouth 1963-10-01 1 Rest

So far, you have seen examples of case expressions in which just one condition within the case expression may occur. Consider some examples that show the other form.

Example 5.22:For each player, find the player number, the year in which he or she joined the club, and the player’s age group.

SELECT PLAYERNO, JOINED, CASE

WHEN JOINED < 1980 THEN 'Seniors' WHEN JOINED < 1983 THEN 'Juniors' ELSE 'Children' END AS AGE_GROUP

Explanation:If the first expression is not true, the next expression is evaluated, then the next, and so on. If none of them is true, the else definition applies.

The advantage of this form of the case expression is that all kinds of conditions can be mixed.

Example 5.23:For each player, find the player number, the year in which he or she joined the club, the town where he or she lives, and a classification.

SELECT PLAYERNO, JOINED, TOWN, CASE

WHEN JOINED >= 1980 AND JOINED <= 1982 THEN 'Seniors'

The result is:

PLAYERNO JOINED TOWN CASE WHEN ...

--- --- --- ---2 1975 Stratford First members 6 1977 Stratford First members 7 1981 Stratford Seniors 8 1980 Inglewood Seniors 27 1983 Eltham Elthammers 28 1983 Midhurst Rest 39 1980 Stratford Seniors 44 1980 Inglewood Seniors 57 1985 Stratford Rest 83 1982 Stratford Seniors 95 1972 Douglas Rest 100 1979 Stratford Rest 104 1984 Eltham Elthammers 112 1984 Plymouth Rest

Case expressions can be used everywhere scalar expressions are allowed, including in the WHEREandHAVINGclauses of the SELECTstatement.

Exercise 5.13: Get the number and the division of each team in which the value

firstis written in full as the first division and the value secondis written in full as the second division. If the value of the division is not first orsecond, display the valueunknown.

Exercise 5.14: Imagine that the tennis club has classified all the penalties in three categories. The category lowcontains all the penalties from 0up and to 40, the categorymoderatecontains those from 41 to80, and the category highcontains all the penalties higher than 80. Next, find for each penalty the payment number, the amount, and the matching category.

Exercise 5.15: Find the numbers of the penalties belonging to the category low (see the previous exercise).

Dans le document SQL for MySQL Developers (Page 128-133)