PDF (A4) - 40.3Mb
Man Pages (TGZ) - 261.9Kb
Man Pages (Zip) - 367.5Kb
Info (Gzip) - 4.0Mb
Info (Zip) - 4.0Mb
You can use theselect() method to query for and return records from a table in a database. The X DevAPI provides additional methods to use with theselect() method to filter and sort the returned records.
MySQL provides the following operators to specify search conditions:OR (||),AND (&&),XOR,IS,NOT,BETWEEN,IN,LIKE,!=,<>,>,>=,<,<=,&,|,<<,>>,+,-,*,/,~, and%.
To issue a query that returns all records from an existing table, use theselect() method without specifying search conditions. The following example selects all records from the city table in theworld_x database.
Limit the use of the emptyselect() method to interactive statements. Always use explicit column-name selections in your application code.
mysql-js> db.city.select()+------+------------+-------------+------------+-------------------------+| ID | Name | CountryCode | District | Info |+------+------------+-------------+------------+-------------------------+| 1 | Kabul | AFG | Kabol |{"Population": 1780000} || 2 | Qandahar | AFG | Qandahar |{"Population": 237500} || 3 | Herat | AFG | Herat |{"Population": 186800} |... ... ... ... ...| 4079 | Rafah | PSE | Rafah |{"Population": 92020} |+------+------- ----+-------------+------------+-------------------------+4082 rows in set (0.01 sec)An empty set (no matching records) returns the following information:
Empty set (0.00 sec)
To issue a query that returns a set of table columns, use theselect() method and specify the columns to return between square brackets. This query returns the Name and CountryCode columns from the city table.
mysql-js> db.city.select(["Name", "CountryCode"])+-------------------+-------------+| Name | CountryCode |+-------------------+-------------+| Kabul | AFG || Qandahar | AFG || Herat | AFG || Mazar-e-Sharif | AFG || Amsterdam | NLD |... ...| Rafah | PSE || Olympia | USA || Little Falls | USA || Happy Valley | USA |+-------------------+-------------+4082 rows in set (0.00 sec) To issue a query that returns rows matching specific search conditions, use thewhere() method to include those conditions. For example, the following example returns the names and country codes of the cities that start with the letter Z.
mysql-js> db.city.select(["Name", "CountryCode"]).where("Name like 'Z%'")+-------------------+-------------+| Name | CountryCode |+-------------------+-------------+| Zaanstad | NLD || Zoetermeer | NLD || Zwolle | NLD || Zenica | BIH || Zagazig | EGY || Zaragoza | ESP || Zamboanga | PHL || Zahedan | IRN || Zanjan | IRN || Zabol | IRN || Zama | JPN || Zhezqazghan | KAZ || Zhengzhou | CHN |... ...| Zeleznogorsk | RUS |+-------------------+-------------+59 rows in set (0.00 sec) You can separate a value from the search condition by using thebind() method. For example, instead of using "Name = 'Z%' " as the condition, substitute a named placeholder consisting of a colon followed by a name that begins with a letter, such asname. Then include the placeholder and value in thebind() method as follows:
mysql-js> db.city.select(["Name", "CountryCode"]). where("Name like :name").bind("name", "Z%")Within a program, binding enables you to specify placeholders in your expressions, which are filled in with values before execution and can benefit from automatic escaping, as appropriate.
Always use binding to sanitize input. Avoid introducing values in queries using string concatenation, which can produce invalid input and, in some cases, can cause security issues.
To issue a query using theAND operator, add the operator between search conditions in thewhere() method.
mysql-js> db.city.select(["Name", "CountryCode"]).where("Name like 'Z%' and CountryCode = 'CHN'")+----------------+-------------+| Name | CountryCode |+----------------+-------------+| Zhengzhou | CHN || Zibo | CHN || Zhangjiakou | CHN || Zhuzhou | CHN || Zhangjiang | CHN || Zigong | CHN || Zaozhuang | CHN |... ...| Zhangjiagang | CHN |+----------------+-------------+22 rows in set (0.01 sec) To specify multiple conditional operators, you can enclose the search conditions in parenthesis to change the operator precedence. The following example demonstrates the placement ofAND andOR operators.
mysql-js> db.city.select(["Name", "CountryCode"]).where("Name like 'Z%' and (CountryCode = 'CHN' or CountryCode = 'RUS')")+-------------------+-------------+| Name | CountryCode |+-------------------+-------------+| Zhengzhou | CHN || Zibo | CHN || Zhangjiakou | CHN || Zhuzhou | CHN |... ...| Zeleznogorsk | RUS |+-------------------+-------------+29 rows in set (0.01 sec) You can apply thelimit(),orderBy(), andoffSet() methods to manage the number and order of records returned by theselect() method.
To specify the number of records included in a result set, append thelimit() method with a value to theselect() method. For example, the following query returns the first five records in the country table.
mysql-js> db.country.select(["Code", "Name"]).limit(5)+------+-------------+| Code | Name |+------+-------------+| ABW | Aruba || AFG | Afghanistan || AGO | Angola || AIA | Anguilla || ALB | Albania |+------+-------------+5 rows in set (0.00 sec) To specify an order for the results, append theorderBy() method to theselect() method. Pass to theorderBy() method a list of one or more columns to sort by and, optionally, the descending (desc) or ascending (asc) attribute as appropriate. Ascending order is the default order type.
For example, the following query sorts all records by the Name column and then returns the first three records in descending order .
mysql-js> db.country.select(["Code", "Name"]).orderBy(["Name desc"]).limit(3)+------+------------+| Code | Name |+------+------------+| ZWE | Zimbabwe || ZMB | Zambia || YUG | Yugoslavia |+------+------------+3 rows in set (0.00 sec) By default, thelimit() method starts from the first record in the table. You can use theoffset() method to change the starting record. For example, to ignore the first record and return the next three records matching the condition, pass to theoffset() method a value of 1.
mysql-js> db.country.select(["Code", "Name"]).orderBy(["Name desc"]).limit(3).offset(1)+------+------------+| Code | Name |+------+------------+| ZMB | Zambia || YUG | Yugoslavia || YEM | Yemen |+------+------------+3 rows in set (0.00 sec)TheMySQL Reference Manual provides detailed documentation on functions and operators.
SeeTableSelectFunction for the full syntax definition.
PDF (A4) - 40.3Mb
Man Pages (TGZ) - 261.9Kb
Man Pages (Zip) - 367.5Kb
Info (Gzip) - 4.0Mb
Info (Zip) - 4.0Mb