Database Articles

  Home arrow Database Articles arrow Page 4 - Power Selecting
DATABASE ARTICLES

Power Selecting
By: bluephoenix
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2004-12-20

    Table of Contents:
  • Power Selecting
  • Order By
  • Distinct
  • As
  • Like
  • Conclusion

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Power Selecting - As


    (Page 4 of 6 )

    Sometimes a column name returned in a query is different from what is expected or what might be needed for a script. As a simple example, consider a query that uses the MySQL function COUNT to return the number of records in a table:

    SELECT COUNT(*) FROM Clients;

    COUNT(*)
    --------
    12

    While COUNT(*) might be a suitable column header for raw output, it wouldn't be so nice to work with as the key of an associative array. Instead, The AS modifier can be used to rename a results column.

    SELECT COUNT(*) AS RecordCount FROM Clients;

    RecordCount
    -----------
    12

    AS only affects the column name of the returned results; it does not actually rename a table column stored within the database, but it can still be a powerful ally when writing complex SELECT queries. Consider the next example which uses the MySQL function CONCAT to combine column data.

    SELECT CONCAT(LastName, ", ", FirstName) AS Name, CONCAT(Address, 
    " ", CITY, ", ", State, " ", Zip) as Address FROM Clients ORDER BY 
    Name ASC;

    Name              Address
    -------------------------------------------------------------
    Adams, Joseph     214 Main St Seattle, WA 98102
    Adams, Gwendolyn  205 W Third St Brownville, AL 35020
    Brown, Michael    2336 NE Cranston Blvd Terre Haute, IN 47814
    Chavez, Jose      14 Quailbush Dr Jasper, TN 37347
    Copland, Dorothy  639 Spring St West Sacramento CA, 95691
    Franklin, Susanna 293 Lamont Dr Tempe, AZ 85283
    Johnson, Diane    82 Richardson Ave Fresno, CA 93702
    LaBelle, Nathan   367 Pineview Ter Skaneateles, NY 13152
    Smith, Aaron      9112 Main St San Diego, CA 92119
    Smith, Harold     321 Elm St Portsmouth, RI 02871
    Smith, Josh       52 Harding Ln Franklin, MA 02038
    Wilson, Jessica   227 Washington Ave Irving, TX 75061

    AS coerces the name of a column to something manageable or in some cases, intelligible. The column names would be quite messy indeed without AS!

    More Database Articles Articles
    More By bluephoenix

    blog comments powered by Disqus

    DATABASE ARTICLES ARTICLES

    - Completing a Book Inventory Management System
    - Uploading Images for a Book Inventory Manage...
    - Finishing the Add Book Story for a Book Inve...
    - Integration Testing for a Book Inventory Man...
    - User Stories for a Book Inventory Management...
    - Unit Testing a Book Inventory Management Sys...
    - Testing a Book Inventory Management System
    - Implementing Models for a Book Inventory Man...
    - Book Inventory Application: Publishers and B...
    - Handling Publishers in a Book Inventory Mana...
    - Publisher Administration for Book Inventory ...
    - Book Inventory Management
    - Using the SQL Reference Manual
    - Using Oracle SQL Developer with SQL Statemen...
    - Fixing Errors with Oracle SQL Developer


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap