|
SQL SyntaxThe most basic SQL syntax for a data-extraction query is: SELECT what FROM table name[, table name ...] [WHERE conditions] what defines what you wish to get from the query. It can be a column name (more on column names later), a function to be performed on the retrieve data (more on this in the functions chapter). The special notation '*' means "all columns from all the specified tables". In order to extract everything from a table, with RXML:
with Pike: string parse (object id) { object db=Sql.sql("mysql://user:password@localhost/sample"); array(mapping) results=db->query("select * from boundaries"); string output="<table border=1>"; foreach (results,mapping m) { output+="<tr><td>"+m->country_1+"<td>"+m->country_2+"<td>"+ m->length+"</tr>\n"; } output+="</table>"; return output;' } If we wanted to get the results only for a column in that table, we would have instead with RXML:
Of course you can select more than one column, simply having what be a comma-separated list of column names. With RXML:
Using a single table doesn't harness the power of relations. Those are not "physical" entities, but are built when a query is executed if multiple tables are specified together with conditions to explain how the data from the tables should be collated (or "the tables are joined"). Usually an equality test is used to specify those conditions, but it's not a requirement. The result of the join operation is a virtual table merging those records from every involved table that satisfy the specified conditions. Let's print the name of the known countries and the geographic regions they belong to. The country names are in the 'ids' table, the regions are in the 'areas' table, the two are tied via the 'countries' table. The relations we'll use are two: ids.code must be equal to countries.country, and countries.map_refs must be equal to areas.id with RXML
Column NamesA column can be addressed in two ways: "plain" and "dotted notation". The latter is the more complete form, and is guaranteed not to be ambiguous. The former is allowed for brevity's sake by most servers (including MySQL), but only when no confusion is possible. Aliases for ColumnsIt is possible (usually to have a function result with a simpler name) to alias the names of the returned columns, simply extending the what parameter above with the syntax column_name AS alias The values will be then available in the result as "alias" column, rather than "column_name". With RXML:
See the functions chapter to see how for an example when using functions. Aliases for TablesTable names can be aliased with the "as" syntax, too. This is especially important in one case, and that is when you need to cross-reference a table with itself, or if a table is involved in multiple relations with another. It's illegal in SQL to have two or more tables with the same name mentioned in the tables list of a query. With our sample database, it's necessary to alias a table if we want to expand the country codes in the boundaries table to their names. In order to accomplish that result, we will need to: With RXML:
Here we aliased two times the 'ids' table for clarity's sake, we could have aliased it only once. Also, we aliased the column names for the same reason. No Tables InvolvedIt is possible to have queries which don't involve any table, simply by not specifying the "FROM" clause. Such queries are not very useful, except sometimes to perform server-assisted translations. With Pike: > object db=Sql.sql("mysql://user:password@localhost/sample"); Result: object; > db->query("select now() as time")[0]->time; Result: "2000-02-29 12:12:57" The conditional part of a query is explained in the following chapter. |
|||||||||||||||