Feb 19

Numeric Functions in MySQL

Database by Imam | No Comments »

A function is a MySQL command used in an SQL statement that takes one or more arguments and returns a value based on the values supplied. Just as with expressions, you can use a function anywhere a fixed value could otherwise be used, or to manipulate the value returned in a database column.

Case Sensitivity
Function names are not case sensitive in MySQL. For example, you can use ROUND(), Round(), or round()these all perform the same function call.

Arguments
To pass a function more than one argument, separate the values in parentheses using a comma. If a function does not require any arguments, the parentheses must still be givenfor instance, RAND().

Random Numbers
To generate a random number, use the RAND() function with no arguments. The result is a random decimal number between 0 and 1.

Popularity: 8% [?]

Feb 18

Numeric Operators in MySQL

Database by Imam | No Comments »

Numeric operators are used in a similar waytwo values appear on either side of a symbol or a sequence of symbols. This expression equates to a new value in the SQL statement.

Operators and Operands
In the expression a + b, the + symbol is the operator and a and b are known as operands.

Using Arithmetic Operators
An expression that uses a numeric operator can be used in a SQL statement anywhere that you could otherwise put a numeric value. You can also use a numeric operator to modify retrieved data from a table, as long as it is numeric data.

You can actually perform a query in MySQL without supplying a table name. This is useful only when you have an expression as a selected value, but it can be used to show the result of an expression on fixed values.

Addition in MySQL is performed using the + operator, and subtraction using the - operator. The following query shows an expression using each of these operators:

mysql> SELECT 15 + 28, 94 - 55;
+——– +———+
| 15 + 28 | 94 - 55 |
+———+———+
| 43 | 39 |
+———+———+
1 row in set (0.00 sec)

A query with no table list returns exactly one row but can contain multiple columns. In this example, the two columns returned contain the results of the two expressions in the

SELECT statement.

The other basic arithmetic operators in MySQL are * and /, for multiplication and division, respectively. The / operator in MySQL returns a decimal numberit does not truncate the result or round it to the nearest whole number.

mysql> SELECT 6 * 8, 72 / 9, 2 / 3;
+——-+——–+——-+
| 6 * 8 | 72 / 9 | 2 / 3 |
+——-+——–+——-+
| 48 | 8.00 | 0.67 |
+——-+——–+——-+
1 row in set (0.00 sec)

Popularity: 7% [?]