• Aucun résultat trouvé

The Compound Numeric Expression

Dans le document SQL for MySQL Developers (Page 143-150)

Common Elements

Exercise 5.18: Get the numbers of the penalties that were paid in 1984

5.13 T HE C OMPOUND S CALAR E XPRESSION

5.13.1 The Compound Numeric Expression

Acompound numeric expressionis a scalar expression that consists of, minimally, a singular scalar numeric expression extended with operators, brackets, and/or other scalar expressions. The result is a scalar value with a numeric data type.

DEFINITION

<compound numeric expression> ::=

[ + | - ] <scalar numeric expression> | ( <scalar numeric expression> ) |

<compound numeric expression>

<mathematical operator> <scalar numeric expression> |

~ <scalar numeric expression> |

<scalar numeric expression>

<bit operator> <scalar numeric expression>

<mathematical operator> ::= * | / | + | - | % | DIV

<bit operator> ::= "|" | & | ^ | << | >>

Consider some examples:

Compound numeric expression Value -- ---14 * 8 112 (-16 + 43) / 3 9 5 * 4 + 2 * 10 40 18E3 + 10E4 118E3 12.6 / 6.3 2.0

Table 5.4 lists the mathematical operators that can be used in a compound numeric expression.

TABLE 5.4 The Mathematical Operators and Their Meanings

Before reading the examples, consider the following comments:

Non-numeric expressions can occur in a compound numeric expression. The only requirement is that the final result of the entire expression must return a numeric value.

If required, brackets can be used in numeric compound expressions to indi-cate the order of execution.

If any component of a numeric compound expression has the value null, the value of the entire expression is, by definition, null.

The calculation of the value of a numeric compound expression is performed in keeping with the following priority rules: (1) left to right, (2) brackets, (3) multiplication and division, (4) addition and subtraction.

Some examples follow (assume that the AMOUNT column has the value 25):

Compound numeric expression Value - ---6 + 4 * 25 10---6 6 + 4 * AMOUNT 106 0.6E1 + 4 * AMOUNT 106 (6 + 4) * 25 250 (50 / 10) * 5 25 50 / (10 * 5) 1 NULL * 30 NULL 18 DIV 5 3 16 * '5' 80

These are incorrect compound numeric expressions:

86 + 'Jim' ((80 + 4) 4/2 (* 3)

MATHEMATICAL OPERATOR MEANING

* Multiply

/ Divide

+ Add

- Subtract

% Modulo

DIV Divide and round off

Example 5.35: Get the match number and the sets won and lost for each match in which the number of sets won is greater than or equal to the number of sets lost multiplied by 2.

SELECT MATCHNO, WON, LOST FROM MATCHES

WHERE WON >= LOST * 2

The result is:

MATCHNO WON LOST --- --- ----1 3 ----1 3 3 0 7 3 0

Explanation: To be able to answer this query, we need the compound numeric expressionLOST * 2.

What are the precision and the scale of the result of a calculation that involves two decimal values? For example, if we multiply a decimal (4,3) by a decimal (8,2), what is the precision and the scale of that result? Here we show the rules that MySQL uses to determine them. We assume that P1 and S1, respectively, are the precision and the scale of the first decimal value, and that P2andS2are those of the second value. In addition, assume that there exists a function called LARGESTthat enables you to determine the largest of two values.

Multiplication—If we multiply two decimals, the scale of the result is equal toS1+S2, and its precision is equal to P1+P2. For example, multiplying a decimal (4,3) with a decimal (5,4) returns a decimal (9,7).

Addition—If we add two decimals, the scale of the result is equal to

LARGEST(S1, S2),and its precision is equal to LARGEST(P1-S1, P2-S2) + LARGEST(S1, S2) + 1. For example, adding a decimal (4,2) to a decimal (7,4) returns a decimal (8,4).

Subtraction—If we subtract a decimal from another, the scale of the result is equal to S1 + S2, and its precision is equal to LARGEST(P1-S1, P2-S2) + LARGEST(S1, S2) + 1. In other words, for subtraction and addition, the same rules apply.

Division—The scale of the result of a division is equal to S1 + 4, and the precision is equal to P1 + 4. For example, if we divide a decimal (4,3) by a decimal (5,4) the result is a decimal (8,6). The value 4can be changed by changing the value of the system variable called DIV_PRECISION_INCREMENT.

Besides the classic mathematical operators, MySQL supports special bit opera-tors;see Table 5.5. Bit operators enable you to work on data on the bit level. Note that bit operators can be used only with scalar expressions that have an integer data type.

TABLE 5.5 Overview of Bit Operators

Example 5.36: Move the number 50two bits to the left.

SELECT 50 << 2

The result is:

50 << 2 ---200

Explanation: Bit operators work on the binary representations of values. The binary representation of the value 50 is110010. With the operator <<, the bits are moved several positions to the left and zeroes are placed at the end. In the previous expression, the value 110010is moved two bits to the left, and that gives 11001000. In the decimal system, this value is equal to 200.

Example 5.37: Move the binary value 11three bits to the left.

SELECT B'11' << 3

The result is:

B'11' << 3 ---24

BITOPERATOR MEANING

| Binary OR

& Binary AND

^ Binary XOR

<< Move bits to the left

>> Move bits to the right

~ Invert bits

To be able to explain how bit operators work, we explain two scalar functions first: the BINandCONVfunctions.Both enable us to get the binary representation for a decimal number.

Example 5.38: Get the binary representation of the values 6and10.

SELECT CONV(6,10,2), CONV(10,10,2), BIN(8), BIN(10)

The result is:

CONV(6,10,2) CONV(10,10,2) BIN(8) BIN(10) --- --- --- ---110 1010 ---110 1010

Explanation: How the BINfunction works is obvious. The parameter value is con-verted to a binary representation. The CONV function is somewhat more complex.

With this function, a value can be converted from one number base into any other.

With CONV(6,10,2), we convert the value 6(according to the decimal base—hence, the number 10) into a binary value (the number 2).

By switching the parameters 10and2, the CONVfunction can also retrieve the corre-sponding decimal number of a binary representation.

Example 5.39:Get the decimal values that belong to the binary representations

1001and111.

SELECT CONV(1001,2,10), CONV(111,2,10)

The result is:

CONV(1001,2,10) CONV(111,2,10) - ---9 7

Consider some more examples of the bit operators. For example, the expression

10 | 6results in 14. The binary representation of 10 is1010, and 6becomes110. When we use the ORoperatoror the |operator, both values are examined bit by bit.

If one of the values or both values has the value 1 on a certain bit position, the resulting value also has a 1on that same position. You could present it as follows as well:

1010 = 10 0110 = 6 ---- |

1110 = 14

With the ANDor the &operator, the left and right values are also compared bit by bit. Only when both values have the value 1on a certain bit position does the result-ing value have a 1on that position. For example, the value of the expression 10 & 6 is equal to 2:

1010 = 10 0110 = 6 ---- &

0010 = 2

Example 5.40:Get the odd player numbers from the PLAYERS table.

SELECT PLAYERNO FROM PLAYERS WHERE PLAYERNO & 1

The result is:

PLAYERNO ---7 27 39 57 83 95

Explanation: A number is odd when a 1occurs on the last position of the binary representation of that number. Applying the &operator on two odd numbers always returns a number that has a 1on the last position of its binary representation. By definition, the result is at least 1. Therefore, the expression PLAYERNO & 1istrueif the player number is odd.

When the XORoperatoror the ^operator is used, the resulting value has a 1on each position where the left or the right value has the value 1on that same position (but not both 0or1). For example, the value of the expression 10 ^ 6is equal to 12.

1010 = 10 0110 = 6 ---- ^

1100 = 12

The << operator is used to move all the bits to the left. For example, in the expression3 << 1, we move the bits one position to the left: 11becomes110then.

The result of this expression is 6. The operator >>can be used to move the bits to the right. The rightmost bit is just cut off. 7>>1has3as a result because the binary representation of 7is111.

When you want to convert all the zeroes to ones and the other way around, use the~operator. Note that this operator works on one scalar expression. The value of

~18446744073709551613is equal to 2. If the value that needs to be moved to the left is too large, all the 1s are moved out of the result, and the value 0is given. For example, the result of both the expressions 50000000000000000000 << 1and5 <<

70is0.

Consider two more examples of SELECTstatements in which these bit operators are used.

Example 5.41: Get the number and the name of each player who has an even player number.

SELECT PLAYERNO, NAME FROM PLAYERS

WHERE PLAYERNO = (PLAYERNO >> 1) << 1

The result is:

PLAYERNO NAME ---

---2 Everett 6 Parmenter 8 NewCastle 28 Collins 44 Baker 100 Parmenter 104 Moorman 112 Bailey

Example 5.42: Apply several binary operators to the columns of the MATCHES table.

SELECT MATCHNO, TEAMNO, MATCHNO | TEAMNO, MATCHNO & TEAMNO, MATCHNO ^ TEAMNO FROM MATCHES

The result is:

MATCHNO TEAMNO MATCHNO | TEAMNO MATCHNO & TEAMNO MATCHNO ^ TEAMNO --- --- ---1 ---1 ---1 ---1 0 2 1 3 0 3 3 1 3 1 2 4 1 5 0 5 5 1 5 1 4 6 1 7 0 7 7 1 7 1 6 8 1 9 0 9 9 2 11 0 11 10 2 10 2 8 11 2 11 2 9 12 2 14 0 14 13 2 15 0 15

Exercise 5.24: Determine the values of the following numeric compound

Dans le document SQL for MySQL Developers (Page 143-150)