Recipe 3.4. Using Column Aliases to Make Programs Easier to Write


Problem

You're trying to refer to a column by name from within a program, but the column is calculated from an expression. Consequently, its name is difficult to use.

Solution

Use an alias to give the column a simpler name.

Discussion

Section 3.3 shows how column aliases make query results more meaningful when you're issuing queries interactively. Aliases also are useful for programming purposes. If you're writing a program that fetches rows into an array and accesses them by numeric column indexes, the presence or absence of column aliases makes no difference because aliases don't change the positions of columns within the result set. However, aliases make a big difference if you're accessing output columns by name because aliases change those names. You can exploit this fact to give your program easier names to work with. For example, if your query displays reformatted message time values from the mail table using the expression DATE_FORMAT(t,'%M %e, %Y'), that expression is also the name you'd have to use when referring to the output column. That's not very convenient. If you use AS date_sent to give the column an alias, you can refer to it a lot more easily using the name date_sent. Here's an example that shows how a Perl DBI script might process such values. It retrieves rows into a hash and refers to column values by name:

$sth = $dbh->prepare ("SELECT srcuser,                        DATE_FORMAT(t,'%M %e, %Y') AS date_sent                        FROM mail"); $sth->execute (); while (my $ref = $sth->fetchrow_hashref ()) {   printf "user: %s, date sent: %s\n", $ref->{srcuser}, $ref->{date_sent}; } 

In Java, you'd do something like this, where the argument to getString⁠(⁠ ⁠ ⁠) names the column containing the value that you want to access:

Statement s = conn.createStatement (); s.executeQuery ("SELECT srcuser,"                 + " DATE_FORMAT(t,'%M %e, %Y') AS date_sent"                 + " FROM mail"); ResultSet rs = s.getResultSet (); while (rs.next ())  // loop through rows of result set {   String name = rs.getString ("srcuser");   String dateSent = rs.getString ("date_sent");   System.out.println ("user: " + name + ", date sent: " + dateSent); } rs.close (); s.close (); 

In Ruby, rows can be fetched as objects in which columns are accessible by either position or name. In PHP, the PEAR DB module enables you to retrieve rows as associative arrays or objects, both of which are data structures that contain named elements. With Python, use a cursor class that causes rows to be returned as dictionaries containing key/value pairs where the keys are the column names.

See Also

Section 2.4 shows for each of our programming languages how to fetch rows into data structures that enable you to access columns values by column name. Also, the select directory of the recipes directory has examples that show how to do this for the mail table.




MySQL Cookbook
MySQL Cookbook
ISBN: 059652708X
EAN: 2147483647
Year: 2004
Pages: 375
Authors: Paul DuBois

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net