Friday, 5 August 2011

Selecting Particular Columns

The next step we can take is to limit the columns that are returned. The * in the previous query (select * from department) means "all the columns in the table." Instead of specifying *, we can list a set of columns we would like returned. This can be a single column, a subset of table columns, or even the complete set of columns in any order that suits us. You should specify the column names as a list of comma-separated values.

For example, the following query selects only the values in the employeeID and name fields of the employee table:

select name, employeeID from employee;

If you run this query on the employee database, you should get a result that looks similar to the following:

+---------------+-------------+
| name | employeeID |
+---------------+-------------+
| Ajay Patel | 6651 |
| Nora Edwards | 7513 |
| Candy Burnett | 9006 |
| Ben Smith | 9842 |
+-------------+---------------+
4 rows in set (0.00 sec)

You can see that only the two columns we specified have been returned. Note that the columns are displayed in the order in which we requested the columns in the query, rather than the order in which they appear in the database schema.

No comments:

Post a Comment