Feb 22

The SELECT Statement

Database by Imam | No Comments »

The SELECT Statement

The first SQL command you will learn, and the one you will use most frequently, is SELECT. In this lesson, you begin by learning how to fetch data records from a single table.

A SELECT statement begins with the SELECT keyword and is used to retrieve information from MySQL database tables. You must specify the table name to fetch data fromusing the FROM keywordand one or more columns that you want to retrieve from that table.

Keywords and Statements

A keyword is a word that is part of the SQL language. In the examples in this book, SQL keywords are always written in capitals, although they are not case sensitive.

A SQL statement begins with a keyword and can contain several more keywords that must appear in the correct, structured wayknown as the statement’s syntax.

Popularity: 9% [?]

Feb 20

Conditional Functions in MySQL

Database by Imam | No Comments »

MySQL also includes a set of functions that return their result based on a condition, just like the conditions you use in a WHERE clause.

TRUE and FALSE
Every condition returns a value, TRUE or FALSE, depending on whether the condition is satisfied. There is no Boolean data type in MySQL TRUE equates to 1, and FALSE equates to 0.

You can actually see these values by executing a condition as a query, as follows:

mysql> SELECT code, price, price > 10.00
-> FROM products;
+——+——-+—————+
| code | price | price > 10.00 |
+——+——-+—————+
| MINI | 5.99 | 0 |
| MIDI | 9.99 | 0 |
| MAXI | 15.99 | 1 |
+——+——-+—————+
3 rows in set (0.00 sec)

The output from this query shows each product and its price from the products table. The conditional column returns TRUE when the price is greater than 10.00.

Popularity: 8% [?]

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% [?]