Sonntag, 28. September 2008

MySQL guide - Querying MySQL tables

Our employee_data table now contains enough data for us to work with. Let us see how we can extract (query) it. Querying involves the use of the MySQL SELECT command.

Data is extracted from the table using the SELECT SQL command. Here is the format of a SELECT statement:

SELECT column_names from table_name [WHERE ...conditions];

The conditions part of the statement is optional (we'll go through this later). Basically, you require to know the column names and the table name from which to extract the data.

For example, in order to extract the first and last names of all employees, issue the following command.

SELECT f_name, l_name from employee_data;

The statement tells MySQL to list all the rows from columns f_name and l_name.

-----
In this section of the MySQL tutorial we'll look at the format of a SELECT statement we met in the last session in detail. We will learn how to use the select statement using the WHERE clause.

SELECT column_names from table_name [WHERE ...conditions];

Now, we know that the conditions are optional (we've seen several examples in the last session... and you would have encountered them in the assignments too).

The SELECT statement without conditions lists all the data in the specified columns. The strength of RDBMS lies in letting you retrieve data based on certain specified conditions.
In this session we'll look at the SQL Comparision Operators.
The = and != comparision operators for MySQL Select

SELECT f_name, l_name from employee_data where f_name = 'John';

+--------+------------+
| f_name | l_name |
+--------+------------+
| John | Hagan |
| John | MacFarland |
+--------+------------+
2 rows in set (0.00 sec)

This displays the first and last names of all employees whose first names are John. Note that the word John in the condition is surrounded by single quotes. You can also use double quotes. The quotes are important since MySQL will throw an error if they are missing. Also, MySQL comparisions are case insensitive; which means "john", "John" or even "JoHn" would work!

SELECT f_name,l_name from employee_data where title="Programmer";

+--------+------------+
| f_name | l_name |
+--------+------------+
| Fred | Kruger |
| John | MacFarland |
| Edward | Sakamuro |
| Alok | Nanda |
+--------+------------+
4 rows in set (0.00 sec)


Selects the first and last names of all employees who are programmers.

SELECT f_name, l_name from employee_data where age = 32;
+---------+--------+
| f_name | l_name |
+---------+--------+
| John | Hagan |
| Ganesh | Pillai |
| Alok | Nanda |
| Arthur | Hoopla |
| Kim | Hunter |
| Shahida | Ali |
+---------+--------+
6 rows in set (0.00 sec)


This lists the first and last names of all empoyees 32 years of age. Remember that the column type of age was int, hence it's not necessary to surround 32 with quotes. This is a subtle difference between text and integer column types.

The != means 'not equal to' and is the opposite of the equality operator.


http://www.webdevelopersnotes.com/tutorials/sql/mysql_guide_querying_mysql_tables.php3

Keine Kommentare: