11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

www.it-ebooks.infoCHAPTER 34 • MYSQL VIEWSSELECT * FROM employee_contact_info_view ORDER BY phone;This produces the following output:+------------+------------+-------------------+------------+| first_name | last_name | email | phone |+------------+------------+-------------------+------------+| Jason | Gilmore | jason@example.com | 2125551212 || Bob | Connors | bob@example.com | 2125559945 || Matt | Wade | matt@example.com | 2125559999 |+------------+------------+-------------------+------------+For that matter, views can be used in conjunction with all clauses and functions, meaning that youcan use SUM(), LOWER(), ORDER BY, GROUP BY, or any other clause or function that strikes your fancy.Passing in ParametersJust as you can manipulate view results by using clauses and functions, you can do so by passing alongparameters as well. For example, suppose that you’re interested in retrieving contact information for aparticular employee, but you can remember only his first name:SELECT * FROM employee_contact_info_view WHERE first_name="Jason";This returns:+------------+-----------+-------------------+------------+| first_name | last_name | email | phone |+------------+-----------+-------------------+------------+| Jason | Gilmore | jason@example.com | 2125551212 |+------------+-----------+-------------------+------------+Modifying the Returned Column NamesTable column-naming conventions are generally a product of programmer convenience, occasionallymaking for cryptic reading when presented to an end user. When using views, you can improve uponthese names by passing column names via the optional column_list parameter. The following exampleis a revision of the employee_contact_info_view view, replacing the default column names withsomething a tad more friendly:CREATE VIEW employee_contact_info_view(`First Name`, `Last Name`, `Email Address`, `Telephone`) ASSELECT first_name, last_name, email, phoneFROM employees ORDER BY last_name ASC;Now execute the following query:SELECT * FROM employee_contact_info_view;663

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!