Database Articles
  Home arrow Database Articles arrow Page 4 - Power Selecting
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Forums Sitemap 
Download TestComplete 
JMSL Numerical Library 
IBM® developerWorks
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
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

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    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


       · No Group bys and havings? :) Good job.
       · Thank you. For those wondering what GROUP BY and HAVING are, they're expressions...
     

    DATABASE ARTICLES ARTICLES

    - More on Query Optimization for Oracle Databa...
    - Query Optimization in Oracle
    - Clusters and Other Data Structures for Oracle
    - Using Indexes with an Oracle Database
    - The Basics of Data Structures in Oracle
    - Oracle Data Structures
    - Best Practices for PL/SQL Variables
    - What`s Code Without Variables?
    - Clauses, Sorting, and SQL Queries
    - The From Clause and SQL Queries
    - Query Primer
    - Full Text Searches and Strings
    - Searching with Strings
    - Pattern Matching with Strings
    - Working with Cases of Strings





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT