So we have these three
different situations.
For the first one find
the ten oldest gorillas.
Well, we're going to need, where,
in order to select out only gorillas.
We're going to need to find the oldest,
so
means we need to put them in
order by their birthdates.
And we want to find the top ten so
that's going to be a limit.
For the second one, list all the animals
in alphabetical order, ten per page.
We're listing all of the animals,
not some of them.
So we,
don't actually need where on this query.
We're listing them in alphabetical order
though, so we're going to need order by.
Since we want to get them ten per page,
we're going to need limit and
offset to put them into separate pages.
For the third situation, find out
which one species we have the most of.
First we're going to need group by,
to bin all the animals by species
using account aggregation.
Then we're going to need order by,
to sort by the result of that count,
and limit to get just the top one.
There's actually another way to do this
using a different SQL feature called
subselects, but with what you've seen so
far, this is how to do it.
لدينا ثلاثة
.مواقف مختلفة
بالنسبة للموقف الأول، علينا العثور
.على أكبر عشر غوريلات عمرًا
حسنًا، نريد شرط where
.حتى يمكن انتقاء الغوريلات فقط
،فنحن بحاجة إلى العثور على الأكبر عمرًا
وهو
ما يعني أنه يلزم ترتيبها
.حسب تواريخ الميلاد
،ونريد العثور على أكبر عشر غوريلات
.ومن ثم سنلجأ إلى استخدام حد
بالنسبة للموقف الثاني، علينا سرد كل الحيوانات
.بترتيب أبجدي بحيث تشمل كل صفحة عشرة حيوانات
،نقوم بسرد كل الحيوانات
.وليس بعضها
،لذا
.لسنا بحاجة إلى أمر where في هذا الاستعلام
نقوم بسردها بترتيب أبجدي
.ولكن سنكون بحاجة إلى أمر ترتيب حسب
،وبما أننا نريد أن تشمل كل صفحة عشرة حيوانات
سنكون بحاجة إلى استخدام حد
.وإزاحة لوضعهما في صفحات منفصلة
بالنسبة للموقف الثالث، اكتشف
.أي نوع من الحيوانات موجود لدينا بكثرة
في البداية، سنكون بحاجة إلى استخدام أمر تجميع حسب
لتجميع كل الحيوانات حسب الأنواع
.باستخدام account aggregation
،بعد ذلك، يلزم استخدام أمر ترتيب حسب
وفرز حسب للنتيجة التي نحصل عليها من ذلك الجرد
.واستخدام الحد للحصول على عشرة فقط
في الواقع، توجد طريقة أخرى للقيام بهذا الأمر
باستخدام ميزة مختلفة من ميزات SQL اسمها
،عمليات التحديد الفرعي. ولكن مع ما عرضناه حتى الآن
.هذه هي طريقة تنفيذ هذا الأمر
Então temos estas três
situações diferentes.
Na primeira situação, encontre
os dez gorilas mais velhos.
Bem, vamos precisar de where
para selecionar apenas gorilas.
Precisamos encontrar o mais velho,
isso significa que precisamos
colocá-los em ordem
por suas datas de nascimento.
E queremos encontrar os dez principais, portanto,
esse será um limite.
Na segunda situação, liste todos os animais
em ordem alfabética, dez por página.
Estamos listando todos os animais,
não alguns deles.
Então,
não precisamos realmente de where nesta consulta.
Mas estamos listando os animais em ordem alfabética,
portanto vamos precisar de order by.
Como queremos colocá-los dez por página,
precisamos de limit e
offset para colocá-los em páginas separadas.
Na terceira situação,
descubra qual espécie temos mais.
Primeiro, precisaremos de group by
para agrupar todos os animais por espécie
usando a agregação de conta.
Então precisaremos de order by
para classificar pelo resultado dessa contagem
e limit para obter apenas o primeiro.
Na verdade, existe outra forma de fazer isso
usando um recurso SQL diferente chamado subselects,
mas com o que já vimos,
essa é a maneira de fazê-lo.
现在我们有这三种不同的情况
第一种情况 找出年纪最大的10只大猩猩
这种情况下 为了只选出大猩猩 我们需要用到 where
我们需要找出年纪最大的一只
这意味着我们需要按照它们的出生日期进行排序
我们要找出排在前10的 这是一个限制
第二种情况 按字母顺序列出所有动物 每页10个
我们要列出所有动物 而不是其中的一些
因此 我们实际上不需要用到查询
但要按字母顺序把它们列出来 因此需要用到 order by 语句
因为我们希望每页列出10个结果 这样就需要用到 limit 和 offset 语句
按页分别列出结果
第三种情况 找出动物数量最多的物种
首先我们需要用到 group by 语句
使用聚合函数 按物种对所有动物进行统计
然后我们需要用到 order by 语句
对统计结果进行分类 并限定只得到排在首位的结果
实际上还有另外一种方法 使用的是一个不同的 SQL 功能
称为 subselect 不过就目前掌握的信息而言 上述方法就很适合