Skip to content

Commit 909749a

Browse files
Merge pull request #885 from ixcat/issue-884
add python-specific order/limit/offset docs parts
2 parents 4b53e5c + 2e61647 commit 909749a

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

docs-parts/queries/03-Fetch_lang1.rst

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,46 @@ Primary key values
3535
3636
``KEY`` can also used when returning attribute values as separate variables, such that one of the returned variables contains the entire primary keys.
3737

38+
Sorting and limiting the results
39+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
40+
41+
To sort the result, use the ``order_by`` keyword argument.
42+
43+
.. code-block:: python
44+
45+
# ascending order:
46+
data = query.fetch(order_by='name')
47+
# descending order:
48+
data = query.fetch(order_by='name desc')
49+
# by name first, year second:
50+
data = query.fetch(order_by=('name desc', 'year'))
51+
# sort by the primary key:
52+
data = query.fetch(order_by='KEY')
53+
# sort by name but for same names order by primary key:
54+
data = query.fetch(order_by=('name', 'KEY desc'))
55+
56+
The ``order_by`` argument can be a string specifying the attribute to sort by. By default the sort is in ascending order. Use ``'attr desc'`` to sort in descending order by attribute ``attr``. The value can also be a sequence of strings, in which case, the sort performed on all the attributes jointly in the order specified.
57+
58+
The special attribute name ``'KEY'`` represents the primary key attributes in order that they appear in the index. Otherwise, this name can be used as any other argument.
59+
60+
If an attribute happens to be a SQL reserved word, it needs to be enclosed in backquotes. For example:
61+
62+
.. code-block:: python
63+
64+
data = query.fetch(order_by='`select` desc')
65+
66+
The ``order_by`` value is eventually passed to the ``ORDER BY`` `clause <https://dev.mysql.com/doc/refman/5.7/en/order-by-optimization.html>`_.
67+
68+
Similarly, the ``limit`` and ``offset`` arguments can be used to limit the result to a subset of entities.
69+
70+
For example, one could do the following:
71+
72+
.. code-block:: python
73+
74+
data = query.fetch(order_by='name', limit=10, offset=5)
75+
76+
Note that an ``offset`` cannot be used without specifying a ``limit`` as well.
77+
3878
Usage with Pandas
3979
~~~~~~~~~~~~~~~~~
4080

0 commit comments

Comments
 (0)