Flylib.com

Books Software

 
 
 

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


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.



Recipe 3.5. Combining Columns to Construct Composite Values

Problem

You want to display values that are constructed from multiple table columns.

Solution

One way to do this is to use CONCAT⁠(⁠ ⁠ ⁠) . You can also give the column a nicer name by using an alias.

Discussion

Column values can be combined to produce composite output values. For example, this expression concatenates srcuser and srchost values into email address format:

CONCAT(srcuser,'@',srchost)

Such expressions tend to produce ugly column names , but you can use column aliases to provide better ones. The following statement uses the aliases sender and recipient to name output columns that are constructed by combining usernames and hostnames into email addresses:

mysql>

SELECT

->

DATE_FORMAT(t,'%M %e, %Y') AS date_sent,

->

CONCAT(srcuser,'@',srchost) AS sender,

->

CONCAT(dstuser,'@',dsthost) AS recipient,

->

size FROM mail;

+--------------+---------------+---------------+---------+
 date_sent     sender         recipient      size    
+--------------+---------------+---------------+---------+
 May 11, 2006  barb@saturn    tricia@mars      58274 
 May 12, 2006  tricia@mars    gene@venus      194925 
 May 12, 2006  phil@mars      phil@saturn       1048 
 May 13, 2006  barb@saturn    tricia@venus       271 
...