Rechercher dans le manuel MySQL
20.4.4.2 Select Tables
You can use the select()
method to query for
and return records from a table in a database. The
X DevAPI provides additional methods to use with the
select()
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 %
.
Select All Records
To issue a query that returns all records from an existing
table, use the select()
method without
specifying search conditions. The following example selects
all records from the city table in the
world_x
database.
Limit the use of the empty select()
method to interactive statements. Always use explicit
column-name selections in your application code.
mysql-py> 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 the
select()
method and specify the columns to
return between square brackets. This query returns the Name
and CountryCode columns from the city table.
mysql-py> 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 the where()
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-py> 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
the bind()
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 as name. Then
include the placeholder and value in the
bind()
method as follows:
mysql-py> 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 the AND
operator, add the operator between search conditions in the
where()
method.
mysql-py> 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
of AND
and
OR
operators.
mysql-py> 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 the limit()
,
order_by()
, and offset()
methods to manage the number and order of records returned by
the select()
method.
To specify the number of records included in a result set,
append the limit()
method with a value to
the select()
method. For example, the
following query returns the first five records in the country
table.
mysql-py> 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 the
order_by()
method to the
select()
method. Pass to the
order_by()
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-py> db.country.select(["Code", "Name"]).order_by(["Name desc"]).limit(3)
+------+------------+
| Code | Name |
+------+------------+
| ZWE | Zimbabwe |
| ZMB | Zambia |
| YUG | Yugoslavia |
+------+------------+
3 rows in set (0.00 sec)
By default, the limit()
method starts from
the first record in the table. You can use the
offset()
method to change the starting
record. For example, to ignore the first record and return the
next three records matching the condition, pass to the
offset()
method a value of 1.
mysql-py> db.country.select(["Code", "Name"]).order_by(["Name desc"]).limit(3).offset(1)
+------+------------+
| Code | Name |
+------+------------+
| ZMB | Zambia |
| YUG | Yugoslavia |
| YEM | Yemen |
+------+------------+
3 rows in set (0.00 sec)
The MySQL Reference Manual provides detailed documentation on functions and operators.
See TableSelectFunction for the full syntax definition.
Deutsche Übersetzung
Sie haben gebeten, diese Seite auf Deutsch zu besuchen. Momentan ist nur die Oberfläche übersetzt, aber noch nicht der gesamte Inhalt.Wenn Sie mir bei Übersetzungen helfen wollen, ist Ihr Beitrag willkommen. Alles, was Sie tun müssen, ist, sich auf der Website zu registrieren und mir eine Nachricht zu schicken, in der Sie gebeten werden, Sie der Gruppe der Übersetzer hinzuzufügen, die Ihnen die Möglichkeit gibt, die gewünschten Seiten zu übersetzen. Ein Link am Ende jeder übersetzten Seite zeigt an, dass Sie der Übersetzer sind und einen Link zu Ihrem Profil haben.
Vielen Dank im Voraus.
Dokument erstellt 26/06/2006, zuletzt geändert 26/10/2018
Quelle des gedruckten Dokuments:https://www.gaudry.be/de/mysql-rf-mysql-shell-tutorial-python-table-select.html
Die Infobro ist eine persönliche Seite, deren Inhalt in meiner alleinigen Verantwortung liegt. Der Text ist unter der CreativeCommons-Lizenz (BY-NC-SA) verfügbar. Weitere Informationen auf die Nutzungsbedingungen und dem Autor.
Referenzen
Diese Verweise und Links verweisen auf Dokumente, die während des Schreibens dieser Seite konsultiert wurden, oder die zusätzliche Informationen liefern können, aber die Autoren dieser Quellen können nicht für den Inhalt dieser Seite verantwortlich gemacht werden.
Der Autor Diese Website ist allein dafür verantwortlich, wie die verschiedenen Konzepte und Freiheiten, die mit den Nachschlagewerken gemacht werden, hier dargestellt werden. Denken Sie daran, dass Sie mehrere Quellinformationen austauschen müssen, um das Risiko von Fehlern zu reduzieren.