Problem Solution Discussion Using Column Aliases to Make Programs Easier to Write

+----------------+------------+ Here, t he value of t he first colum n is 1+1+1 quot ed so t hat it is t reat ed as a st ring , and t he value of t he second colum n is 1+1+1 wit hout quot es so t hat MySQL t reat s it as an expression and evaluat es it . The aliases are descript ive phrases t hat help t o m ake clear t he relat ionship bet ween t he t wo colum n values. I f you t ry using a single- word alias and MySQL com plains about it , t he alias probably is a reserved word. Quot ing it should m ake it legal: mysql SELECT 1 AS INTEGER; You have an error in your SQL syntax near INTEGER at line 1 mysql SELECT 1 AS INTEGER; +---------+ | INTEGER | +---------+ | 1 | +---------+

3.5 Using Column Aliases to Make Programs Easier to Write

3.5.1 Problem

Youre t rying t o refer t o a colum n by nam e from wit hin a program , but t he colum n is calculat ed from an expression. Consequent ly, it s difficult t o use.

3.5.2 Solution

Use an alias t o give t he colum n a sim pler nam e.

3.5.3 Discussion

I f youre writ ing a program t hat fet ches rows int o an array and accesses t hem by num eric colum n indexes, t he presence or absence of colum n aliases m akes no difference, because aliases dont change t he posit ions of colum ns wit hin t he result set . However, aliases m ake a big difference if youre accessing out put colum ns by nam e, because aliases change t hose nam es. You can exploit t his fact t o give your program easier nam es t o work wit h. For exam ple, if your query displays reform at t ed m essage t im e values from t he mail t able using t he expression DATE_FORMATt,M e, Y , t hat expression is also t he nam e youd have t o use when referring t o t he out put colum n. That s not very convenient . I f you use AS date_sent t o give t he colum n an alias, you can refer t o it a lot m ore easily using t he nam e date_sent . Heres an exam ple t hat shows how a Perl DBI script m ight process such values: sth = dbh-prepare SELECT srcuser, DATE_FORMATt,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}; } I n Java, youd do som et hing like t his: Statement s = conn.createStatement ; s.executeQuery SELECT srcuser, + DATE_FORMATt,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 ; I n PHP, ret rieve result set rows using mysql_fetch_array or mysql_fetch_object t o fet ch rows int o a dat a st ruct ure t hat cont ains nam ed elem ent s. Wit h Pyt hon, use a cursor class t hat causes rows t o be ret urned as dict ionaries cont aining key value pairs where t he keys are t he colum n nam es. See Recipe 2.5 .

3.6 Combining Columns to Construct Composite Values